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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

General Questions

Status
Not open for further replies.

Ciralia

Programmer
Oct 22, 2002
51
0
0
US
I have seen some coding techniques that I am not familiar with, so I hope some of you can shed some light on them:

std::string variableA //what is the purpose of using std here?

//Can someone explain how this code actually works? I tried to name the methods and variables accordingly.
NamespaceA::ClassA::ClassA(const NamespaceA::ClassA& other) : variableB(other.variableB)
{
}

//Here is another one that confuses me. It is a method declaration.
bool operator()(const NamespaceA::ClassA* c1, const NamespaceA::ClassA* c2) const;

The last question I have: what does c_str() do?
 
std is the namespace where all STL code is located. So the full name of a string is std::string.

NamespaceA is a custom namespace that someone created.
ClassA is obviously a class, and the 2nd ClassA is a constructor for that class. In this case, it's a copy constructor because it takes a const reference to another ClassA object.

The following is a constructor initialization list:
Code:
: variableB( other.variableB )
variableB is a member variable of ClassA which is being set to the value of variableB from the 'other' class which is passed to the copy constructor.

operator() is a functor. It allows you to pass an object (lets call it 'obj') to a function, and then that function call the functor like this:
Code:
obj( c1, c2 );
assuming that c1 and c2 are ClassA objects...
Basically it's the C++ way for passing function pointers.

c_str() is a function of std::string which returns a const char* version of the string.

If you pick up a good C++ programming book like "Beginning Visual C++" by Ivor Horton, it will explain all those concepts in a more concise way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top