Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

:  member_function or : member_property ?

Posted on 1998-03-13
8
Medium Priority
?
212 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

670 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