-3

I am reading a C++ textbook for teaching myself programming.

Now I have a question with a sentance in the fallowing paragraph

which is underlined in red.

enter image description here

I don't understand what does that sentance mean.

Using class methods(class function members) outside the class declaration and method definitions.

How to use class methods(class function members) outside method definitions?

What are special measures?

Could anyone help me with this?

Thanks in advance.

Stats Cruncher
  • 155
  • 1
  • 1
  • 6
  • "which we'll get to soon"... To me it sounds like the are just wanting to use some vague and big sounding language before telling you that they'll just tell you the details later. Keep reading (or find a different book). – crashmstr Oct 01 '15 at 12:09
  • @user9418 - Do they mean `static` methods? Keep reading and see what the book says! – Phorce Oct 01 '15 at 12:09
  • 5
    "... requires special measures, which we'll get to soon". Turn the page! – vladon Oct 01 '15 at 12:09
  • The textbook is "C++ Primer Plus" written by Stephen Prata. – Stats Cruncher Oct 01 '15 at 12:14
  • FInd a [better book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Andrew Oct 01 '15 at 12:21
  • I have "C++ Primer" as well. Most of the time I read "C++ Primer Plus" and reference to "C++ Primer". – Stats Cruncher Oct 01 '15 at 12:28
  • @Phorce - Actually I have read through the whole book. After that I still don't understand what does that sentance mean. – Stats Cruncher Oct 01 '15 at 12:33
  • @user9418 Continue reading.. This should become more clear to you :) + There are good answers – Phorce Oct 01 '15 at 13:21

2 Answers2

0

That means if you use it in some other method of Stock you don't need the class scope Stock::Update(), for example

void Stock::Foo()
{
    // Do stuff

    Update(10.0);   // This is Stock::Update()
}

But if you called it in some function outside the method of the Stock class, you would need an instance of Stock to call the method from, because it is non-static.

void Bar()
{
    Stock stock;

    // Do stuff

    stock.Update(10.0);    // This will find the Stock:: scope
}

This also means that the method Update has to be public for you to call it in the above example Bar. If it were private, you could only call the Update method from other Stock methods.

class Stock
{
public:
    void Update(double price);
};
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

I think basically it means that in order to use this class method from other code (main method, other class method body, etc) you need additional measures. With this it means basically that you have to make this class visible to the scope where you are trying to use it (in general by including the header where it's defined) so you are able to instance variables/objects of this class and to call its methods on them.

For example, in order to use this class (let's call it foo) from other code you have to do the following:

#include "foo.h"

void MyClass::bar()
{
     Foo instanceOfFoo;
     instanceOfFoo.Update();
     ....
}

If you need to call the Update() method from the Foo class .cpp file there's no need to create an instance of itself. So you can call its methods with these additional measures. So the Foo class methods could call the Update like the following:

void Foo::SomeMethod()
{
    Update(); // 
}

In fact the above method has a hidden parameted called this which refers to the current instance of the Foo class. So the code above is something like:

void Foo::SomeMethod(Foo* const this)
{
    this->Update(); // 
}
rkachach
  • 16,517
  • 6
  • 42
  • 66