简体   繁体   中英

How to define class constructor inside another class header file?

This is a basic question but gives me hard time. I have class A and in the header file I want to define another class constructor B from another header file. I tried this code below which I'm sure that's not the correct way.

Ah

class A{
public:
    A();
    B b();  //Constructor from another Class that defined in another header file
    void Operation();
};

I need to call the constructor B in Ah so I can call the constructor B inside constructor A and also use function in Class B inside A::Operation() .

A.cpp

#include "A.h"
#include "B.h"
A::A{
    b();  //call constructor b
}

void A::Operation(){
    b.someFunction();  //use function from class B, error here
}

As expected, the error I got is at b.someFunction()

expression must have class type

Anyone know how to define another class's constructor inside another class header file properly? And call the other constructor inside main class constructor and use the other class's function globally? Sorry for basic and confusing question.

This is not a constructor:

 class A{ public: A(); B b(); //This is a function named b, returning an object of type B void Operation(); }; 

Same here:

 A::A{ b(); //call function b from above, that returns a B object } 

And same here:

 void A::Operation(){ b.someFunction(); // Without invoking method b, you apply the dot operator on the method - which is illegal. } 

You probably want an object of type B, and call someFunction method on it. Maybe you want:

class A{
public:
    A();
    B b; // this is object named b, of type B
    void Operation();
};

And then, if the constructor of B requires parameters, you can:

A::A () : b(constructor parameters) {
}

If there is no need to pass parameters, you can just omit the construction of b, and the language will simply use the default constructor of B (without parameters).

A::A ()  {
}

The way to invoke or call a constructor of B is to create an instance of B .

The process of creating an instance of B involves calling/invoking a constructor of B . The implementation (aka compiler) takes care of the mechanics of calling the constructor in the process of creating an object. The job of the constructor is to ensure the object is initialised for subsequent use.

Practically speaking, it does not make sense to call a constructor in any context other than object construction. There are some advanced use cases in which a constructor is effectively called manually (eg using a placement new expression to initialise a block of memory so it contains an object). However that is done to create an object from the specified memory. This is completely different from what you are seeking.

To construct a B within (code in) a header file ah , the header file ah needs to provide visibility of a declaration of B and its constructors.

For example, assuming bh declares the class B , the header ah might do

#include "b.h"

class A
{
   public:
       A() : b()  {} ;
       void Operation();
   private:
       B b;      //  A contains an instance of B
};

In the above (working bottom up) the declaration B b specifies that A has a member of type B , named b .

The definition of A s constructor

A() : b()  {} ;

uses an initialiser list to initialise (construct) that member b . This assumes the class B has a constructor that can accept no arguments.

The declaration you had within class A (which I have removed from the sample I provided above)

 B b(); //Constructor from another Class that defined in another header file 

is not what you describe. It is actually a declaration of a member function of class A , which is named b() and returns a B . Calling such a function generally requires B to have a working constructor (typically a copy or move constructor). Such a declaration, however, is not a constructor of B .

just #include "Bh" in Ah and have B b; object in Ah .

#include "B.h"
class A{
  A();
  B b;
  void operation();
}

this way the compiler will have all the information needed from class B constructors and other functions as well for this compilation unit A.cpp and linker will do its job later linking the function name to its logic in other compilation unit B.cpp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM