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!

Template class - function prototype question

Status
Not open for further replies.

Aubryn

Programmer
Apr 19, 2005
4
0
0
US
I've been looking at some sample code that is similar to a program I'm working on, and I came across a function prototype in the class header that threw me off. Can anyone here explain the syntax of this?

template<class Type>
class Row
{
public:
Row(int cols=0):row(NULL) {SetRowSize(cols);}
...

After Row(int cols=0), what does the :row(NULL) do? I'm confused, anyone able to help?
 
Oh yeah, forgot to put the private data members in.

private:
int numCols;
Type* row;


So the row being referred to in my question above is a pointer, if that helps clarify. Is it checking if the pointer is NULL, and if so, what does it do if true/false, etc?
 
It's constructor's member initialization: set member var row to NULL. Read more (and more;) about C++: it's a common and ordinal C++ construct...
 
Ok, well that makes sense. Still learning how to code, as you can tell. =/

Now my issue is, does it have to go in the prototype? I like to seperate my prototype from my implementation code, unlike the example shown.
For example, I tried something like this, and its giving me a ton of errors...

template<class Type>
class Row
{
public:
Row(int cols=0):row(NULL)
...
};

Row(int cols=0):row(NULL)
{
SetRowSize(cols);
}

Also, could I just put row=NULL; after my SetRowSize(cols) statement, and still have the same results?
 
Use init member list only for a constructor implementation, not in a declaration. So
Code:
// Declaration:
...
class X
{
public:
  X(int x);
...
  Type* row;
  int   m_x;
...
};
// Implementation:
...
X::X(int x):row(0),m_x(x)
// or
X::X(int x)
{
  m_x = x;
  row = 0;
}
Yes, the same effect...
 
I forget to add in that case. You can't initialize members with non-trivial constructors via assignment in a constructor. In these cases use member init list.
 
It has the same effect, but using member initializers are more efficient because it doesn't need to create a temporary variable to hold the value, it just sets the member variable directly.
i.e.
Code:
ClassA::ClassA() : m_Row( 0 )
is more efficient than
Code:
ClassA::ClassA()
{ m_Row = 0; }
 
Great, I understand now. Appreciate the help guys. :)
 
cpjust,
it's an interesting question about effectiveness. No any temporary variables in m_Row = 0 compiled code. A direct setting in a class member initializer is an illusion in most cases. Imagine direct setting of a dynamic or stack class object allocation (created in run time) - it's an old good assignment of (old good;) constant to a member variable slot (exactly the same as an assignment expression). In all cases the same class constructor works...
I think a list initialization in simple (data types) cases is a preferred method in pragmatic sense (to initialize? let's initialize!), but not for effectiveness...
 
Maybe your compiler is smart enough to optimize the code in a way to remove the temporary variables?
Or maybe I gave a bad example. Here's one that really shows the use of the temporary variables:
Code:
ClassA::ClassA( int i )  // i gets put directly into m_Row.
: m_Row( i ) {}
Code:
ClassA::ClassA( int i )  // i gets created, then assigned to m_Row.
{ m_Row = i; }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top