I don't even... please explain the source
May 4, 2010 at 7:09pm UTC
This has to do with pointers and references.
Please explain what does the
const do right after
int GetAge() ? (line 13)
FunctionTwo is a constant pointer to constant
SimpleCat object, am I correct? (line 36)
What kind of role does the line 37 play?
The lines:
1 2 3 4
13: int GetAge() const { return itsAge; }
...
36: const SimpleCat * const FunctionTwo
37: (const SimpleCat * const theCat);
The entire source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
1: //Listing 9.10 - Passing pointers to objects
2:
3: #include <iostream>
4:
5: using namespace std;
6: class SimpleCat
7: {
8: public :
9: SimpleCat();
10: SimpleCat(SimpleCat&);
11: ~SimpleCat();
12:
13: int GetAge() const { return itsAge; }
14: void SetAge(int age) { itsAge = age; }
15:
16: private :
17: int itsAge;
18: };
19:
20: SimpleCat::SimpleCat()
21: {
22: cout << “Simple Cat Constructor...” << endl;
23: itsAge = 1;
24: }
25:
26: SimpleCat::SimpleCat(SimpleCat&)
27: {
28: cout << “Simple Cat Copy Constructor...” << endl;
29: }
30:
31: SimpleCat::~SimpleCat()
32: {
33: cout << “Simple Cat Destructor...” << endl;
34: }
35:
36: const SimpleCat * const FunctionTwo
37: (const SimpleCat * const theCat);
38:
39: int main()
40: {
41: cout << “Making a cat...” << endl;
42: SimpleCat Frisky;
43: cout << “Frisky is “ ;
44: cout << Frisky.GetAge();
45: cout << “ years old” << endl;
46: int age = 5;
47: Frisky.SetAge(age);
48: cout << “Frisky is “ ;
49: cout << Frisky.GetAge();
50: cout << “ years old” << endl;
51: cout << “Calling FunctionTwo...” << endl;
52: FunctionTwo(&Frisky);
53: cout << “Frisky is “ ;
54: cout << Frisky.GetAge();
55: cout << “ years old” << endl;
56: return 0;
57: }
58:
59: // functionTwo, passes a const pointer
60: const SimpleCat * const FunctionTwo
61: (const SimpleCat * const theCat)
62: {
63: cout << “Function Two. Returning...” << endl;
64: cout << “Frisky is now “ << theCat->GetAge();
65: cout << “ years old “ << endl;
66: // theCat->SetAge(8); const!
67: return theCat;
68: }
Last edited on May 4, 2010 at 8:47pm UTC
May 4, 2010 at 9:01pm UTC
13: int GetAge() const { return itsAge; }
A
const class member function, tells everyone (the compiler and other users of your classs)
that that function does NOT modify any class member variables (either directly or indirectly).
1 2
36: const SimpleCat * const FunctionTwo
37: (const SimpleCat * const theCat);
Actually this is a function declaration split over two lines.
When put on the same line it looks like this:
const SimpleCat * const FunctionTwo (const SimpleCat * const theCat);
It is a prototype for a function taking a parameter of a
constant pointer to a constant SimpleCat and returning a
constant pointer to a constant SimpleCat
Last edited on May 4, 2010 at 9:01pm UTC
May 5, 2010 at 3:31pm UTC
T ha nks .
Last edited on May 5, 2010 at 3:32pm UTC
May 5, 2010 at 3:47pm UTC
A const class member function, tells everyone (the compiler and other users of your classs)
that that function does NOT modify any class member variables (either directly or indirectly).
Unless you declare a variable as
mutable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
cclass Example
{
public :
mutable int x; // mutable tells that variable can be modified by const functions
void set_x(int y) const ;
};
void Example::set_x(int y) const
{
x=y;
}
int main()
{
Example e;
e.set_x(10);
cout <<e.x<<endl;
}
May 5, 2010 at 5:55pm UTC
Does it matter where I place the
const keyword within function prototype/definition?
int GetAge() const { return itsAge; }
void set_x(int y) const ;
May 5, 2010 at 9:29pm UTC
null wrote:Unless you declare a variable as mutable
I forgot about mutable..
May 5, 2010 at 9:35pm UTC
neshtak wrote:Does it matter where I place the const keyword within function prototype/definition?
1 2 3 4 5 6 7 8 9 10 11 12
class SomeClass
{
void inlined_function() const {}
void not_inlined_function() const ;
};
void SomeClass::not_inlined_function() const
{
}
May 6, 2010 at 1:15am UTC
Right.
I see it now.
T ha n k s .
Topic archived. No new replies allowed.