2

Let's say that I have two classes A and B.

class A { private: int value; public: A(int v) { value = v; } }; class B { private: A value; public: B() { // Here's my problem } } 

I guess it's something basic but I don't know how to call A's constructor.

Also the compiler demands a default constructor for class A. But if A has a default constructor than wouldn't the default constructor be called whenever I declare a variable of type A. Can I still call a constructor after the default constructor has been called? Or can I declare an instance of a class and then call a constructor later?

I think this could be solved using pointers but can that be avoided ?

I know that you can do something like this in C#.

EDIT: I want to do some computations in the constructor and than initialize the classes. I don't know the values beforehand.

Let's say I'm reading the value from a file and then I initialize A accordingly.

4 Answers 4

13

The term you are looking for is initializer list. It is separated from the constructor body with a colon and determines how the members are initialized. Always prefer initialization to assignment when possible.

class A { int value; public: A(int value) : value(value) {} }; class B { A a; public: B(int value) : a(value) {} } 

I want to do some computations in the constructor and than initialize the classes. I don't know the values beforehand.

Simply perform the computation in a separate function which you then call in the initializer list.

class B { A a; static int some_computation() { return 42; } public: B() : a(some_computation()) {} } 
Sign up to request clarification or add additional context in comments.

3 Comments

Don't you need copy constructor in A for this?
@Manoj: What makes you think so? I don't see any place in my code where an A object is copied.
Oh yes. Indeed you don't copy constructor. I got little confused at a(some_computation()).
3

You use an initialization list to initialize the member variables.

public: B() : value(4) { // calls A::A(4) for value. } 

Comments

0

Or can I declare an instance of a class and then call a constructor later?

Yes, you can do that. Instead of (A value;) declare (A* value;), and then B's constructor will be B():value(new A(5)){}.

In the B's destructor you will have to do delete value;

I think this could be solved using pointers but can that be avoided ?

Yes. Use shared_ptr.

2 Comments

Oh my god, this is SO not C++! When in Rome, do as the Romans do. Don't use pointers unless you absolutely have to.
Well, you can do this, but I don't see why you would. If you will always need to store an A, use a variable A a, not A* a. That way you avoid having to allocate an A on the heap, which may also incur memory leakage if you forgot to delete it in the destructor. So do as in FredOverflow's answer. It's faster and less error-prone.
0

Try:

 B() : value(0) { // Here's my problem } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.