logic seeker
logic seeker

Reputation: 105

Elaborated Type specifiers for class type

This is regarding the question

A class name introduced inside a class is not treated as a nested class name.

I am confused why Part1 and Part2 are different with respect to §3.3.2 [basic.scope.pdecl]/p7 of the standard:

Both are Elaborated Type specifiers and should fall into same section But it seems that there scope seems to be different

Part 1:

struct A 
{
    struct B;
    B* m_b;
};

Part 2:

struct A 
{
    struct B* m_b;
};

Please guide me in this.

Upvotes: 0

Views: 215

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69884

Maybe this little example will help to elaborate:

#include <iostream>

struct B {

    void call() { std::cout << "B" << std::endl; }
};

struct A {

    struct B* p;
};

struct A2 {

    struct B;
    struct B* p;
};

struct A2::B {
    void call() { std::cout << "A2::B" << std::endl; }
};

int main()
{
    B pb;
    A2::B pb2;

    auto a = A { &pb };

    a.p->call();

    auto a2 = A2 { &pb2 };
    a2.p->call();

    return 0;
}

expected results:

B
A2::B

Summary:

A::p is a B*

A2::p is a A2::B*

A2::B and B are entirely different classes

Upvotes: 2

Related Questions