Solved

:  member_function or : member_property ?

Posted on 1998-03-13
8
204 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
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
Opps, I was pretty close,  I don't know what is wrong with me today.  Now I got to type it in again!
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hope you don't mind waiting for the points - network trouble
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now