Class with no default constructor as a member in another class?

H

Henrik Goldman

Hi,

Lets say I have a class a which looks like the following:

class a
{
public:
a(int i)
{
m_nI = i;
}
private:
int m_nI;
};

This class does not have a default constructor. Now I'd like to use the
class in class b as a member variable:

class b
{

private:
a m_a;
};

However since a needs a parameter to get constructed how can I specify this
as a part of class b? I know it's possible to initialize a by using a
pointer as member variable and e.g. new it inside the constructor but I'd
like to have a normal stack based variable.

How do I go about this?

Thanks in advance.

-- Henrik
 
S

Salt_Peter

Henrik said:
Hi,

Lets say I have a class a which looks like the following:
<snip>
try:

class a
{
int m_nl;
public:
a( int i ) : m_nl( i ) { }
};

Thats called an init list.
This class does not have a default constructor. Now I'd like to use the
class in class b as a member variable:

class b
{
private:
a m_a;
};

class b
{
a member;
public:
b() : member( 0 ) { }
b( int i ) : member( i ) { }
~b() { }
};

Thats 2 ctors for the price of one + a d~tor.
If you Capitalized your Classes (class A instead of class a), you'ld
find it easier to observe what is a type and variable of that type.
However since a needs a parameter to get constructed how can I specify this
as a part of class b? I know it's possible to initialize a by using a
pointer as member variable and e.g. new it inside the constructor but I'd
like to have a normal stack based variable.

How do I go about this?

The init list lets you initialize the member(s).
 
E

Earl Purple

Salt_Peter said:
class b
{
a member;
public:
b() : member( 0 ) { }
b( int i ) : member( i ) { }
~b() { }
};

Thats 2 ctors for the price of one + a d~tor.

No need for the destructor. One constructor would do:

class b
{
a member;
public:
explicit b( int i ) : member( i )
{
}
};

of course in reality you might put the implementation of b's
constructor in a different file thus:

class b
{
a member;
public:
explicit b( int i );
};

// then somewhere else
b::b( int i ) : a( i )
{}

If you Capitalized your Classes (class A instead of class a), you'ld
find it easier to observe what is a type and variable of that type.

Style preference irrelevant here. Many prefer not to capitalise
classes. But of course in reality you'd give them more meaningful names
than a and b.
 
S

Salt_Peter

Earl said:
No need for the destructor. One constructor would do:

I disagree, its quite important to indicates one's intentions in so far
as the d~tor is concerned:
consider:

class b
{
...
public:
virtual ~b() { }
};

Add to that the need to investigate proper destruction of objects:

#include <iostream>

class b
{
...
public:
...
~b() { std::cout << "~b()\n"; }
};
class b
{
a member;
public:
explicit b( int i ) : member( i )
{
}
};

of course in reality you might put the implementation of b's
constructor in a different file thus:

class b
{
a member;
public:
explicit b( int i );
};

// then somewhere else
b::b( int i ) : a( i )
{}

that would not compile. try...
// b.cpp
#include "b.h"

b::b( int i ) : member( i )
{
}
Style preference irrelevant here. Many prefer not to capitalise
classes. But of course in reality you'd give them more meaningful names
than a and b.

point taken, but then why did you make the mistake of writing:

b::b( int i ) : a( i )
{}

hmm?
 
H

Henrik Goldman

Thanks for the suggestions.

In real life I naturally use better class names then a and b and I also
start them with capital names (with a prefixed C):

class CMyTestClass
{

};

-- Henrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top