Cannot access public constant member variable in main

Hello everyone,

My code is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Base {
 ...
}

class Derived : public Base {
public:
 const int SOME_VAL; // which is initialized later in the Constructor 
                     // smth like: Derived::Derived : SOME_VAL(1) {...}

private:
 ...
}


#include "Derived.h"
int main() {
 Base* b = new Derived();
 int val = b.SOME_VAL; // b-Obj does not see the SOME_VAL constant
}


Maybe there are some trivial things I am missing.
I tried to "google" it; however, I am not getting anything similar to what I am trying to achieve.

Please, let me know what I am doing wrong.
Thank you in advance!
Last edited on
You are using the wrong syntax to access the member.
b->SOME_VAL;

||

*b.SOME_VAL;

The second first returns the memory location that the pointer stores, then resolves the variable. This is ugly, consider using the first, but is sometimes necessary when using iterators or accessing user defined operators on a class object, since the compiler acts on the pointer and not the object itself.
*b.SOME_VAL;

Needs to be (*b).SOME_VAL;

*b.SOME_VAL is interpreted by the compiler as "give me the content of SOME_VAL (which must be a pointer) which is a member of object b (which is not a pointer)" because of operator precedence

But yes, do b->SOME_VAL
Last edited on
b-Obj does not see the SOME_VAL constant
Yes, it's a pointer to Base which doesn't have SOME_VAL
I had to try this for myself, and yep you guys are right, thats a pointer.
All pointer rules apply when you need to call instance variables in that class.

Also note that you're pointing to a Base class and not a Derived class, so you'll still fail becuase as coder777 mentioned, thats points to Base which doesnt have SOME_VAL. you're going to need an instance of Derived as well as a pointer to the Derived() class.


1
2
    Derived* b = new Derived();
    int val = b->SOME_VAL;


BUT, how are you going to initialize the const int SOME_VAL?
something like this in the Derive.h?

const int Derived::SOME_VAL = 42;

or you'll need to declare it static in your Derive class definition,
Last edited on

Base* b = new Derived();

after the statement above b has type pointer to Base. So in the expression

b->SOME_VAL;

the compiler will search SOME_VAL in the definition of Base. Because there is no such a member as SOME_VAL in class Base the compiler will issue an error.
Hello everyone and thank you very much for your replies!

First, I am very sorry for the mistyped part with b.SOME_VAL. Actually, it should be b->SOME_VAL.
Since it was 2 a.m., and I was not copy/past-ing the code, I did that mistake :) ; so, my apologies for that.

@coder777
Thanks to coder777, "I was blind, yet know I see."
I am starting to realize the issue regarding the "pointer to Base which doesn't have SOME_VAL", and that is why b-Object does not see the SOME_VAL.

However, can you explain me why the b-Object can see the methods within the class Derived?
THE CODE IS BELOW.
By the way, class Base is an abstract class.

@LGonzales
The const int SOME_VAL is initialized in Derived.cpp in the constructor.
Derived::Derived() : SOME_VAL(1) {...}


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
/*Base.h*/
class Base {
 ...
 virtual void someMethod() const = 0;
 ...
}
///////////////////////////////////////////////////////////
/*Derived.h*/
class Derived : public Base {
public:
 int getSomeVal() const; // returns SOME_VAL
 const int SOME_VAL;

private:
 ...
}
///////////////////////////////////////////////////////////
/*main*/
#include "Derived.h"
int main() {
 Base* b = new Derived();
 
 int val1 = b->SOME_VAL; // OK, I understand why b-Obj does not see the SOME_VAL; 
                                                // b-Obj refers to Base
 int val2 = b->getSomeVal(); // Why this works then?
}


Thank you in advance guys!
Last edited on
You are wrong. The statement

int val2 = b->getSomeVal(); // Why this works then?

shall not be compiled because class Base has no such a method (as it follows from the definition of Base).

It is the static type of a pointer that defines which members exist.
Last edited on
Because you've declared the Derived::getSomeVal() method to be virtual. This allows the program to work out which actual class is being pointed to by b at run-time. This is polymorphism in action!

I assume that your Base class declares a getSomeVal() method in its interface, even if it doesn't define one?

If you don't know what polymorphism is, then I strongly recommend you go to your textbook and try and get your head around it. It's absolutely fundamental to object-oriented development.
I got it!

@MikeyBoy, @vlad_from_moscow
Thank both of you. MikeyBoy is right, I have a virtual method getSomeVal() in my Base class. I generally understand how polymorphism works, yet I have to practice it more plus pay and be more attentive.

Thank you again!
Last edited on
You're welcome :)
Topic archived. No new replies allowed.