Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Apr 24, 2000

Constructor for a Protected Class

Question:
How do I create a constructor for a protected member of a class? I have declared the function in the header file but not defined the definition. The error message I get from the compiler is below:
make test_Message
CC -c Message.cc 
"Message.h", line 50: Error: Could not find 
Message::Message(const char[11], String, int) to initialize base 
class.
1 Error(s) detected.
*** Error code 1
make: Fatal error: Command failed for target `Message.o'
The line where the error message is pointing to is to an individual message and I believe the error message is saying it can't initialize the base class message.

Answer:
You can declare a constructor protected only in base classes or classes that should have no instances (for example, abstract classes). A derived class can access all the protected members of its public or protected base class. Therefore, it can invoke the base class' constructor even if it's protected. However, a containing class doesn't have this privilege and is therefore prohibited from accessing a non-public constructor of its member object. Either declare your constructor public, or make it a base class.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date