DMaster
DMaster

Reputation: 641

What is object.classname kind?

Consider this class:

struct Foo {
  int bar;
  Foo(){}
  Foo(int a) {
    bar = a;
  }
};

Now, I have two ways to access member bar:

Foo foo(1234);
cout << foo.bar << endl;
cout << foo.Foo::bar << endl;

But I don't know what is foo.Foo's kind:

using namespace foo.Foo; //error, because foo.Foo is not a namespace
foo.Foo baz; //error, because foo.Foo is not a class

Please tell me what is foo.Foo.

Upvotes: 0

Views: 45

Answers (3)

Othman Benchekroun
Othman Benchekroun

Reputation: 2018

It is not (foo.Foo)::bar, it's foo.(Foo::bar) and Foo::bar is bar

This can be usefull in this example :

struct A{
    int i;
};
struct B{
    int i;
};

class C : public A, B{
};

int main(){
    C c;
    c.i = 0;   // request for member 'i' is ambiguous
}

you should use namespaces A and B to access the data, A::i or B::i

Upvotes: 1

mystic_coder
mystic_coder

Reputation: 472

Members of struct are public by default , so you can access memebrs with dot and well as scope resolution (::) operator.So foo.Foo::bar is same as foo.bar.

Upvotes: 0

TartanLlama
TartanLlama

Reputation: 65610

foo.Foo isn't anything. Foo::bar is the bar member of the Foo class, and foo.Foo::bar is the bar member of the Foo class in the instance foo.

That syntax is generally used if you have name conflicts with multiple inheritance. Consider this example:

struct A{int bar;};
struct B{int bar;};
struct C:A,B{};

Now the following code is ambiguous:

C c;
std::cout << c.bar;

We need to say which bar we want:

C c;
std::cout << c.A::bar;
std::cout << c.B::bar;

Upvotes: 3

Related Questions