private constructors

T

Telos

Ok, having trouble figuring out why this doesn't compile:

class A
{
public:
A( int x ) : _x( x ){};

private:
int _x;

A();
};

class B : public A
{
public:
B( A *anA ) : _a( anA ) {};

private:
A *_a;

};


It says that B does not have access to A's default constructor, but
this shouldn't be an issue because B shouldn't be constructing an A
anyway. It should only be taking a pointer to a pre-existing A.

I've tried it with two different compilers... so I guess this is
proper behavior, but I would like to understand why.
 
J

John Harrison

Ok, having trouble figuring out why this doesn't compile:

class A
{
public:
A( int x ) : _x( x ){};

Illegal semi-colon.
private:
int _x;

A();
};

class B : public A
{
public:
B( A *anA ) : _a( anA ) {};

Another illegal semi-colon.
private:
A *_a;

};


It says that B does not have access to A's default constructor, but
this shouldn't be an issue because B shouldn't be constructing an A
anyway.

Yes it should.
It should only be taking a pointer to a pre-existing A.

I've tried it with two different compilers... so I guess this is
proper behavior, but I would like to understand why.

B is derived from A. Therefore every B object must construct an A object.
You haven't specifed in your B constructor how you want the A object to be
constructed, therefore your code attempts to use the default constructor
which is not available.

You could fix this in several ways, for instance by not derived B from A.
Or by modifying your code something like this

public:
B( A *anA ) : A(999), _a( anA ) {}

or this

public:
B( A *anA ) : A(*anA), _a( anA ) {}

I guess you didn't really mean to derive B from A since you don't realise
that this implies that every B object contains within it an A sub-object.

john
 
S

Sharad Kala

John Harrison said:
Illegal semi-colon.

I don't think calling the semicolon illegal is correct. It's true that
placing the semicolon is unnecessary but at the same time the code is going
to compile. Illegal to me would mean that it won't compile in the first
place.

-Sharad
 
J

John Harrison

I don't think calling the semicolon illegal is correct. It's true that
placing the semicolon is unnecessary but at the same time the code is
going
to compile. Illegal to me would mean that it won't compile in the first
place.

-Sharad

You're right. I checked the C++ grammar and a semi colon is optional
there. I always thought it was something that C++ compilers allowed
because it was a common mistake, but I guess not.

john
 
T

Telos

John Harrison said:
Another illegal semi-colon.

If the semi-colons were the problem, the compiler would have made that
clear to me... heh. Honestly it's been so long since I wrote any code
I couldn't remember if they were supposed to be there or not, so I
erred on the side of caution. Since it compiled I assumed I was
right. Interesting that it's optional...

I guess you didn't really mean to derive B from A since you don't realise
that this implies that every B object contains within it an A sub-object.


Yeah. I actually realized this about half an hour after posting. I
didn't want B to be derived from A at all, which was preventing me
from thinking about the implications of inheritance. I'm not sure why
I did derive it... but I guess that's what you get for writing code at
2am. :(

The sad part is that when I typed it the example I gave you guys, I
still did the derivation without thinking about it...

Oh well, sorry about that and thanks for the responses. :)
 
J

jeffc

Telos said:
Ok, having trouble figuring out why this doesn't compile:

class A
{
public:
A( int x ) : _x( x ){};

private:
int _x;

A();
};

class B : public A
{
public:
B( A *anA ) : _a( anA ) {};

private:
A *_a;

};


It says that B does not have access to A's default constructor, but
this shouldn't be an issue because B shouldn't be constructing an A
anyway.

I'll give you a hint. This doesn't compile either (same error)
class A
{
public:
A( int x ) : _x( x ){};

private:
int _x;

A();
};

class B : public A
{
};
 
G

Gottfried Eibner

John said:
You're right. I checked the C++ grammar and a semi colon is optional
there. I always thought it was something that C++ compilers allowed
because it was a common mistake, but I guess not.

john

You are guessing right. Semicolons just end statements. And if you ever
write a compiler, you will put an empty statement in your grammar, too
(this is common practice).
So you can put any semicolon anywhere out in the code without getting any
errors. ;o)

gottfried
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,479
Latest member
JaysonK723

Latest Threads

Top