request for member of non-class type

Z

ztycho

Could someone explain if the following gcc 4.1 compile error is
working as designed and, if so, why does the second version of the
code not fail?

Failing case:
======================================
struct A
{
int GetId() const;
};

struct B
{
A& GetElement() const;
};

template<typename T>
int function(const B& b, const T&)
{
return dynamic_cast<const A&>(b.GetElement()).GetId();
}

test.cpp: In function 'int function(const B&, const T&)':
test.cpp:14: error: request for member 'GetId' in 'dynamic_cast<const
A&>(b->B::GetElement())', which is of non-class type 'const A&'
=========================================

Working case:
=========================================
struct A
{
int GetId() const;
};

struct B
{
A& GetElement() const;
};

template<typename T>
int function(const B& b, const T&)
{
const A& element = dynamic_cast<const A&>(b.GetElement());
return element.GetId();
}
=========================================


Any help would be appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top