Advertisement

Object member does not name a type C++

Started by November 14, 2015 06:26 PM
12 comments, last by Khatharr 9 years, 1 month ago

if you can help me: in composition, can a subclass access the private members of the base class? Because i'm trying to do so yet i get an error saying "cPerson is private".
No you cannot normally, private things are private. If you have things you want to share with your derived cousins, you can use "protected".

Another option is to make a "friend" class, which can probe anything, if I remember correctly (have used it once or twice in all the years), but such use is highly discouraged. Usually, its use often indicates something is wrong with your class hierarchy.

Using ";" at the end of a function is personal choice now, right? xD I sometimes do that, i don't know why.
Declarations (class, enum) require it, functions do not. It can be confusing at times :)

Just make sure you don't add stray semi-colons in your code bodies, as you insert additional empty statements then, which may cause a few surprises at some points.

if you can help me: in composition, can a subclass access the private members of the base class? Because i'm trying to do so yet i get an error saying "cPerson is private".
No you cannot normally, private things are private. If you have things you want to share with your derived cousins, you can use "protected".

Another option is to make a "friend" class, which can probe anything, if I remember correctly (have used it once or twice in all the years), but such use is highly discouraged. Usually, its use often indicates something is wrong with your class hierarchy.

Using ";" at the end of a function is personal choice now, right? xD I sometimes do that, i don't know why.
Declarations (class, enum) require it, functions do not. It can be confusing at times smile.png

Just make sure you don't add stray semi-colons in your code bodies, as you insert additional empty statements then, which may cause a few surprises at some points.

For some reason i can't even access protected memebers (i tried this already).

"inavlid use of non-static data member PP::number"

I think i cannot access protected members when i use composition...good to know :)

Advertisement
"inavlid use of non-static data member PP::number"

That's not an access rights message, that's static versus non-static data.

The non-static case is


class N {
public:
    int n;   // Only exists inside objects of type N.
};

The N class has an integer named n. Since n has no 'static' property, it's a data member that exists only in an object of type N. It also means you have an 'n' in each object:


N p; // Create an object named p of type N
p.n = 4; // And you can access the 'n' of that object.

N q;
q.n = 3;  // Is different from p.n, so p.n is not affected by this assignment.

However, N::n does not exist, since n only exists inside an object. "int g() { return N::n; }" fails, as you need an object for n to exist.

The static data member case is


class N2 {
public:
    static int n2;  // Exists as static data member. There is only one N2::n2 in the entire program.
                    // You can access it even if no objects of N2 type exist.
};

int f() {
  return N2::n2;  // Works, because N2::n2 always exists.
}

In short, n2 always exists, but there is only one such variable.

n exists in only in objects of its class, but you can have many of them (one in each object).

n2 could also be accessed from objects of type N2 in that case, but they would all be referring to the same member variable.

Example:


#include <iostream>

class CountAware {
public:
  CountAware() {count++;}
  static int getCount() {return count;}
private:
  static int count;
};

int CountAware::count = 0;

int main() {
  CountAware a;
  std::cout << a.getCount() << "\n";
  CountAware b;
  std::cout << b.getCount() << "\n";
  CountAware c;
  std::cout << c.getCount() << "\n";
  std::cout << CountAware::getCount() << "\n\n";
  
  system("pause");
}

1

2

3

3

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement