Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling inherited constructors

Reply
Thread Tools

calling inherited constructors

 
 
Joe, G.I.
Guest
Posts: n/a
 
      09-22-2008
I didn't write ClassA, but I'm trying to inherit from ClassA through
ClassB. I want to pass a string to the ClassA constructor but not sure
I'm calling ClassA's constructor correctly. I get the following g++
error ...

In constructor 'ClassB::ClassB(String&)':
../../../include/ClassB.h:19: error: expected `{' at end of input


ClassB *myClass = new ClassB("myClassName");

// ClassB.h
class ClassB : public ClassA {
ClassB(String &name) : ClassA(name);
}

// ClassB.cpp
ClassB::ClassB(String &name)
{
}

// ClassA constructor which I'm trying to pass the string to ...
ClassA::ClassA (const String & name)



What am I doing wrong here?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-23-2008
Joe, G.I. wrote:
> I didn't write ClassA, but I'm trying to inherit from ClassA through
> ClassB. I want to pass a string to the ClassA constructor but not sure
> I'm calling ClassA's constructor correctly. I get the following g++
> error ...
>
> In constructor 'ClassB::ClassB(String&)':
> ../../../include/ClassB.h:19: error: expected `{' at end of input
>
>
> ClassB *myClass = new ClassB("myClassName");
>
> // ClassB.h
> class ClassB : public ClassA {
> ClassB(String &name) : ClassA(name);
> }
>

class ClassB : public ClassA
{
ClassB(String &name);
};

> // ClassB.cpp
> ClassB::ClassB(String &name)
> {
> }
>

ClassB::ClassB(String &name)
: ClassA( name )
{
}

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      09-23-2008
"Joe, G.I." <invalid.email@address> wrote in
news:(E-Mail Removed):

> I didn't write ClassA, but I'm trying to inherit from ClassA through
> ClassB. I want to pass a string to the ClassA constructor but not sure
> I'm calling ClassA's constructor correctly. I get the following g++
> error ...
>
> In constructor 'ClassB::ClassB(String&)':
> ../../../include/ClassB.h:19: error: expected `{' at end of input
>
>
> ClassB *myClass = new ClassB("myClassName");
>
> // ClassB.h
> class ClassB : public ClassA {
> ClassB(String &name) : ClassA(name);
> }
>
> // ClassB.cpp
> ClassB::ClassB(String &name)
> {
> }
>
> // ClassA constructor which I'm trying to pass the string to ...
> ClassA::ClassA (const String & name)
>
>
>
> What am I doing wrong here?


Assuming you fix the minor issues (like the missing semicolon at the end of
the class declaration), the piece you've got wrong is that the initializer
list goes with the constructor definition, not the declaration:

class ClassB : public ClassA {
ClassB(String & name);
};

ClassB::ClassB(String & name) : ClassA(name) {}


Something else to consider... do you really want to pass the String by non-
const reference? Generally speaking, if you're passing stuff by reference,
you should probably pass it by const-reference (unless you have a specific
reason to pass it non-const...)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Syntax of constructors of inherited classes calling the original constructor lars.uffmann@rwth-aachen.de C++ 20 12-01-2005 03:50 PM
Calling Inherited Constructors Justin C++ 6 11-04-2005 07:56 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments