By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

Instantiate class with non default constructor

P: n/a
I have two classes ClassA and ClassB.

ClassB has a constructor that has ClassA as it's parameter. How do I make
ClassB as a member object of ClassA so that ClassA (i.e. 'this') is passed
into ClassB on instatiation?
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Richard wrote:
I have two classes ClassA and ClassB.

ClassB has a constructor that has ClassA as it's parameter. How do I make
ClassB as a member object of ClassA so that ClassA (i.e. 'this') is passed
into ClassB on instatiation?


class ClassA
{
public:
ClassA() : b(this) {}
private:
ClassB b;
};

Some compilers will give you warnings about this code because you are
using the this pointer before the ClassA object is constructed. But
provided you don't try and dereference the pointer in the ClassB
constructor it is OK.

john
Nov 22 '05 #2

P: n/a
Your name better not be Richard Stallman...

Nov 22 '05 #3

P: n/a
"twoeyedhuman1111" <tw**************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Your name better not be Richard Stallman...


Nope my name is Richard Head.
Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.