Solved

inline function problems

Posted on 1998-05-21
19
251 Views
Last Modified: 2011-09-20

//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.
0
Comment
Question by:gavinpat
  • 12
  • 6
19 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 70 total points
ID: 1164455
Answer comming...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164456
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...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164457
"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();

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164458
The above was only an example.  Obviously it will give undefined results because you never assigned a value to beam::Columns.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164459
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

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164460
>> All members of a const object ARE indded const inside a const member function.
const member functions can be invoked on non-const objects.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164461
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.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164462
>> 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.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164463
Anyways, the real bug was the "int StartBeams = Beams();" statement.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164464
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.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164465
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.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164466
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.

0
 
LVL 11

Expert Comment

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

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164468
Microsoft Agents ... Agents of the Devil !!! :-)

Shall we call you Merlin, or Robbie?

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164469
>> Shall we call you Merlin, or Robbie?
Does not compute.  Both names are not in database.  Please explain.
0
 

Author Comment

by:gavinpat
ID: 1164470
int   StartBeams = Beams();

is within a member function of a class derived from the beam class
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164471
Great.  Get rid of the cast, fix the spelling error.  Still got problems?

0
 
LVL 10

Expert Comment

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

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164473
And the autograder hits again!
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

  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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

16 Experts available now in Live!

Get 1:1 Help Now