Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

base class reference in inheritance? 2

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
hi all,

going through a beginning c++ book and ran into an issue with inheritance. i want to take the existing class "string" and expand on it. i was going to create a new class called "text" with the following in a "text.h" file...

Code:
class text : public string
{
....
};

and then the function definitions in a "text.cpp" file. however, when i try to build the project (using visual studio.net), i get an error "string: base class undefined".

i tried doing an &quot;#include <string>&quot; in various places with no resolution. what am i doing wrong?

thanks,

glenn
 
i've seen that syntax here and there, but have never seen an explanation about when and where to use it. the book doesn't mention anything about needing to use that syntax when inheriting from a base class. can you explain for me?

also, if i include the class declaration within the main source file, it works aok. the problem is when i try to create a separate .cpp and .h file for the class declaration and definition.

thanks,

glenn
 
ok, i got it. so the std::string is used when you haven't declared &quot;using namespace std;&quot; at the top? i added this line to the text.h file and it runs aok. thanks for the tip. any advantage to using &quot;std::string&quot; over &quot;using namespace std&quot;?

glenn
 
I'd say the advantage to use std::string is that the code becomes clearer. You could use another sdk (foo) which may also have a string, and you could have your own string. By using
std::string, foo::string or string you'd know at a glance which string you use.
 
Think of a namespace as a box of classes, functions, variables, constants, etc. Namespace &quot;std&quot; is the standard namespace.

By saying &quot;using namespace std;&quot; you basically dump out the contents of the box all over the floor. That lets you use anything that was in the box, but it's messy, and, as PerFnurt mentions, can cause conflicts between names with other libraries and with your own classes.

By saying &quot;using std::string;&quot; you just take out what you want from that box. It's a lot neater, more explicit, clearer, and causes fewer conflicts.

It's usually ok to just use &quot;using namespace std;&quot; in your beginning projects, but keep in mind what it does.
 
namespace is a fairly new addition to C++ to solve to age old problem of name/scope conflict resolution.

C++ How to Program by Deitel & Deitel 3rd Ed. Ch 21 talks about this.

Also goto google.com and enter &quot;C++ namespace&quot; to learn more than you want to know about it.

Ain't life grand !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top