what does this do?

D

Danny

struct B: public A
{
int & b
B(int & a) : A(a), b(a){}
};

Is A(a), b(a) calling the constructor
I have never seen this syntax.

Would this be equivilent to

struct B: public A
{
int & b
B(int & a) : A(a), b(a){
super.A(a)
b=a
}
};

Thanks,

danny
 
V

Victor Bazarov

Danny said:
struct B: public A
{
int & b
^^^
A semicolon is probably missing here
B(int & a) : A(a), b(a){}
};

Is A(a), b(a) calling the constructor
I have never seen this syntax.

It's called "initialiser list". What book on C++ are you reading
that doesn't explain it?
Would this be equivilent to

struct B: public A
{
int & b
B(int & a) : A(a), b(a){
super.A(a)
b=a
}
};

No. There's no such thing as "super." in C++ and references cannot
be re-seated. And you seem to be missing all the semicolons except
the last one.

V
 
C

codigo

Danny said:
struct B: public A
{
int & b
B(int & a) : A(a), b(a){}
};

Is A(a), b(a) calling the constructor

A(a) invokes a constructor, it doesn't "call" it.
I have never seen this syntax.

Would this be equivilent to

struct B: public A
{
int & b
B(int & a) : A(a), b(a){
super.A(a)
b=a
}
};

Thanks,

danny

Victor is right, you are missing the semi-colons to end the statements and
super isn't part of C++. However, the code you supplied is close to the Java
equivalent of a C++ class except you would be stating "super(a);" and Java
can't pass_by_reference. Since Java can only inherit from 1 base class,
super designates that base class (A).

No such restriction exists in C++. Any number of parent classes and
interfaces(pure-virtual) can be derived from.
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top