Solved

:  member_function or : member_property ?

Posted on 1998-03-13
8
207 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
HTTPSendRequest with WinINet delays on first call 11 28
DCT of  2D array using fftw in c++ 9 60
Need some help with mailto 16 20
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

791 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