473,413 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

request for member of non-class type

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
Aug 11 '08 #1
1 5565
On Aug 11, 4:39*pm, ztycho <zty...@gmail.comwrote:
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
Could this be related?
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34364

Aug 11 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Tim Martin | last post by:
Hi, I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me a bit. The author states: 'C++ has no "methods." Java and Smalltalk have methods. When you talk about an...
5
by: Nimmi Srivastav | last post by:
When I was learning C, I learned that the data type of a function name by itself is a function pointer. For example int someFunction(char* str) { .... }
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
4
by: Morgan Cheng | last post by:
I tried to build a CThread on linux which imitate the behavior of java Thread. I think this is still in the scope of C++. First, I tried this way #include <pthread.h> class CThread { public:...
4
by: SP | last post by:
Hi I have a problem with a customer's XML being submitting to me in a non-well-format XML. They said that there are 18 other partners who has been able to tweak the XML to make it work. So I...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
3
by: cs | last post by:
Hi, I'm new to C and would appreciate any feedback on the following program, asplit, which splits a file into 2 new files, putting a certain number of lines in the first file, and all the rest...
5
by: James R. Davis | last post by:
I am building a set of shared functions and subroutines in a vb file located in my App_Code section of my application. The first three functions went fine and work as expected. But I am now...
4
by: Earl Partridge | last post by:
Using this line of code: Dim q = From Member In BirthdaysDataSet.Member Select Member.MName I'm told that it "Reference to a non-shared member requires an object reference". The dataset exists in...
5
by: hnshashi | last post by:
I have writtem kernel(2.4) module to commu. with user space appl. using netlink socket. I am getting compilation error. kernel module:-> #include <linux/skbuff.h> #include<linux/module.h> ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.