Solved

:  member_function or : member_property ?

Posted on 1998-03-13
8
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Technology Partners: 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!

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

631 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