Solved

:  member_function or : member_property ?

Posted on 1998-03-13
8
208 Views
Last Modified: 2010-04-10
I've got a class declaration which goes like this:

template <class TEMPLATE_TYPE>      
class ALT_MATRIX_ROW      
{
      TEMPLATE_TYPE*  pRow;
        ALT_MATRIX_ROW( int C ) : Columns( C )
      {}

      public      :
            int      Columns;                    
            TEMPLATE_TYPE& operator[]( int i )      
            {
#ifdef AL_DEBUG
            if( i < 0 || i >= Columns )
              {
                  cerr << "ERROR : i Out of Range ALT_MATRIX_ROW::[ " << i << " ]\n";
                        cin.get();
                  }
#endif
                  return( pRow[ i ] );
            }

      void Output( ostream &s );      // "Output" prototype as public of ALT_MATRIX_ROW

            friend class ALT_MATRIX<TEMPLATE_TYPE>;
};

Does anyone know whether statements of the form below are limited to calling member functions eg the member function Columns(C) or can be used to directly manipulate member properties. ie what does the following statement do.

ALT_MATRIX_ROW( int C ) : Columns( C )
      {}

I'd also like to hear of any textbooks which cover class functionality to the level of class templates, friends as class templates, far pointers etc
0
Comment
Question by:gavinpat
  • 6
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1183429
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183430
Sorry,  I thought I had the answer and had it all typed up and went to paste some of your code in an realised I miss-read it.  Now I can't understand you code.  Is it typed correctly?  

A good book that discusses templates is "The C++ programming Language" by Bjarne Stroustrup.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183431
Opps, I was pretty close,  I don't know what is wrong with me today.  Now I got to type it in again!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1183432
Its hard to read because of the funky indenting (at least on my browser).

the line

ALT_MATRIX_ROW( int C ) : Columns( C )  {}

is just a constructor for the (template) class ALT_MATRIX_ROW.  (I put template in quotes because the fact that ALT_MATRIX_ROW is a template class, as opposed to a regular class, does not matter.  A regular class would have the same syntax for its constructor.)  

What it does is sets the "Columns" member to the integer value specified in the parameter.  

I think you may be really confussed (I was too, although there is no excuse in my case).  You said

Does anyone know whether statements of the form below are limited to calling member functions eg the member function.

No member functions are being called here.  In a constructor (and only a constructor) you can intialize the class's members and base classes by placing a colon after the end parenthesis, then listing the members to be initialized with their parameters in parenthesis.  This is followed by the left brace that starts the function.

example coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183433
give the class

Class
{
 int  Int
 char Char
 double Double;
}

we can write the constructor for it like

Class:Class(int i,char c,double d) :
  Int(i),
  Char(c),
  Double(d)
{
}

this is the same as

Class:Class(int i,char c,double d) :
{
  Int = i;
  Char =c;
  Double = d;
}

technically there is a difference, in the first case the three members are constructed right away to their propper values.  In the second case they are default constructed with their default contructors and then set to new values with the = operator.  (However none of the simple types have default constructors, so it ends up being the same in this case).

Note that this only works with constructors.  Not other functions.  You cannot do

Class::SetInt(int i) :
   Int(i)  // not allowed
{
};

You must do

Class::SetInt(int i) :
{
 Int = i;
};
0
 

Expert Comment

by:jann
ID: 1183434
Read "Effective C++", I use this book as a bible.  In that book, the author mentioned when you should use assignment (=) and when to use initialization.  He strongly recommended initialization because sometime assignment could call too many constructors, copy constractors, and destructors, which will slow down your compiling speed.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183435
Agreed--in general.  However for POD (plain old data, that is simple types) there is no default constructor and there is no difference.  In addition, an optimizer will usually convert assignement operators to constructors (when it knows that assignment operators really assigns, it could do other things).

The Effective C++ books are by Scott meyers.  There are two and they are essential reading.
0
 

Author Comment

by:gavinpat
ID: 1183436
Hope you don't mind waiting for the points - network trouble
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question