Link to home
Start Free TrialLog in
Avatar of gavinpat
gavinpat

asked on

inline function problems


//in beam.h file

Class beam
{
private:
int Columns;
int rows;
public:
---------------
//other member functions here
---------------
 int      Beams() const
            { return( (int)Columns ); }      
};

//in beam.cpp file

-------------
int   StartBeams = Beams();
-------------

As far as I can see Columns is cast from a const int to int and then returned but although this compiles and runs fine in Borland C++5 Microsoft VC5 doesn’t like it. When I go to debug the application I get to this function  and nothing seems to be visible to  the debugger (or the function itself judging by the output) not even the this pointer. StartBeams remains set to the value it was initialized to.
But Bjarne Stroustrup has many of this type of inlined function within class declarations so I can’t see where I am going wrong, I’m hoping I don’t have to “inline” all these functions explicitely.
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You have several misconseption here.  Let's go over them one by one:

First, Columns is an int, not a const int.  As you wrote:
    int Columns;

The member function beam::Beams() is declared "const".  That means it is guaranteed not to change the object it is invoked upon (thus is can operate on a const beam).

Therefore, the cast is unnecessary.

More...
"Class beam" is a syntax error.  it should have been lowercase "class beam".

Also, it is customary to put the public part before the private.  After all, this is the interface your class exposes to the world.

So your class will look something like:

    class beam
    {
    public:
        // Other member functions...
        int Beams() const { return Columns; }
    private:
        int Columns;
        // Other member variables...
    };

You'll need a function to set the "Columns" variable too.  Probably a constructor.  Maybe a destructor.  Other functions too...

Also, let's look at the .CPP file:

    int StartBeams = Beams();

You want to invoke the member function beam::Beams()?  You must have a beam object for it to operate on.

Try something like:

    beam MyBeam;
    int StartBeams = MyBeam.Beams();

The above was only an example.  Obviously it will give undefined results because you never assigned a value to beam::Columns.
Avatar of RONSLOW
RONSLOW

I don't think that you explaination is quite right.

gavinpat is quite correct.  All members of a const object ARE indded const inside a const member function.  So Columns IS a 'const int' inside the Beams() function (you cannot say Columns++ for example).

However, there is no problem with casting a 'const int' to an 'int'.  The return value is effectively just an assignment.  It is no different from saying
  int x = 1;
here you are assigning a const int (1) to an int x.  Saying
  return Columns;
is not different.

Also, you do not need the explicit casting you have used (never cast when you don't need to).  You can simply say
  int Beams () const { return Columns; }

What there WOULD be a problem in doing is casting a 'const int&' to an 'int&' eg.
  int& Beams() const { return Columns; }
which fails because you are casting a const int reference to an int reference.  You could then use the returned reference to change the const value of Columns (which is why this is not allowed).

Similarly when working with pointers.  So
  int* Beams () const { return &Columns; }
is wrong.

But your code is fine.  I am using VC5 an have no problems at all with your code (from what I could tell).  I wrote this...

class beam {
private:
  int Columns;
  int rows;
public:
  int Beams() const { return (int)Columns; }
  void f();
};

void beam::f(void) {
  int StartBeams = Beams();
}

void main(void) {
  beam x;
  x.f();
}

This compiles fine (of course, Columns in uninitialised so has a rubbish value in it, but that doesn't matter for this example).

Perhaps there is more going on than you code extract is showing.

I think you need to send more complete code as the problems does not seem to be in the code you have shown us.

you can send me code at Roger_Onslow@compsys.com.au

>> All members of a const object ARE indded const inside a const member function.
const member functions can be invoked on non-const objects.
That is beside the point.  Within a const function ALL members are const.

eg.

class X {
  int m;
public:
  void f() const {
    m++;
  }
}

void main () {
  X x;
  x.f();
}

It doesn't matter that 'x' above is const or not .. member variable m in function f is const .. that is why you cannot change it.  Indeed .. 'this' is always a pointer to a const object within a const function.

>> That is beside the point.  Within a const function ALL members are const.
That too is beside the point.  "Columns" is returned by value so no cast is needed.
Anyways, the real bug was the "int StartBeams = Beams();" statement.
Alexo: Why was that "the real bug" ?  I would have assumed that (because it was in beam.cpp) that it was part of the implementation for a member function of the beam class.  If in a memeber function of the beam class, then you don't need an object, as that is implied.

Alexo: Your points about requiring other functions are quite true, but not really relevant to the problem at hand.  In any case, gavinpat did say that there were other member functions etc.  so he probably does construct his class and set up his variables.

Unfortunately, none of what you said really explains the problem or answers the question (correctly).

The reason why nothing seems to be visible to the debugger is that you probably have inline functions enabled for debug builds.  Turn it off, and you'll be able to debug into the 'inline' function.

The reason why the const int to int can happen implicitly during the return is that you are returning values rather than references or pointers (in which case a cast WUOLD be required if that was really what you wanted to do).

BTW: Gavinpat: In general, you may find it useful to split your inline functions into several lines so that you can see each one when single stepping and are able to more easily set breakpoints.  eg.
  int Beams() const {
    return Columns;
  }
rather than
  int Beams() const { return Columns; }
(in your sample in the question you do have multiple lines .. but ensure other functions that you may want to debug/trace are written over multiple lines).

PS: A reminder... If you have found my comments to be more accurate, relevant or helpful etc, then you will need to reject the currently proposed answer so that I can submit an answer for you to grade.

The code seems to be incomplete.  However, my points were (and still are):
0. "Class" is a syntax error.
1. The cast is not needed.
2. The "Columns" member is not initialized.
3. "int StartBeams = Beams()" will not work [outside of a member function -- correction accepted].

>> Unfortunately, none of what you said really explains the problem or answers the question.
I can be wrong occasionally (as a wise man once said: "... we are people").
If that is in fact so, gavinpat can reject my answer.

We are indeed people .. well, at least, I know I am, but there could be some good AI programs out there pretending to be experts.  Perhaps while you sleep, your computer logs in to experts exchange under another expert Id and answers questions?  Didn't one of the experts go away on holiday and come back to find that he had 'answered' a question?  Spooky.

BTW: I thought I was wrong once myself .. but I was mistaken.  [now _there's_ an old joke :-)]

Anyway, the grading of the question is now in the hands of the gods .. well, gavinpat at least.

>> there could be some good AI programs out there pretending to be experts.
Actually, we prefer to be called "agents".

Microsoft Agents ... Agents of the Devil !!! :-)

Shall we call you Merlin, or Robbie?

>> Shall we call you Merlin, or Robbie?
Does not compute.  Both names are not in database.  Please explain.
Avatar of gavinpat

ASKER

int   StartBeams = Beams();

is within a member function of a class derived from the beam class
Great.  Get rid of the cast, fix the spelling error.  Still got problems?

Have my comments about why const int -> int is ok without a cast and how to see function from the debugger helped you?

And the autograder hits again!