Solved

inline function problems

Posted on 1998-05-21
19
252 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

911 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

19 Experts available now in Live!

Get 1:1 Help Now