473,385 Members | 1,474 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,385 software developers and data experts.

Strange pimpl problem - const correctness not respected

Hello!

I have a strange problem with my pimpl implementation. What happens is that
const correctness is not respected. Let me go straight to the code:

#include <iostream>

struct Geometry {};

class LineData
{
class LineDataImpl;
LineDataImpl& mImpl;

public:

LineData();

const Geometry& GetGeometry() const;
/* */ Geometry& GetGeometry() /* */;
};

class LineData::LineDataImpl
{
Geometry mGeometry;

public:

const Geometry& GetGeometry() const
{
std::cout << "Calling const overload" << std::endl;
return mGeometry;
}

Geometry& GetGeometry()
{
std::cout << "Calling non-const overload" << std::endl;
return mGeometry;
}
};

LineData::LineData()
: mImpl(*(new LineDataImpl))
{ }

const Geometry& LineData::GetGeometry() const
{
return mImpl.GetGeometry();
}

Geometry& LineData::GetGeometry()
{
return mImpl.GetGeometry();
}

int main()
{
const LineData lineData;
const Geometry& geometry = lineData.GetGeometry();

return 0;
}

This sample shows that despite my intentions of being const correct, the
compiler ends up calling the non-const overload. What is going on here?
I'm using Visual Studio 2008, SP1.

Thanks in advance!

--
Daniel Lidström

Oct 14 '08 #1
3 1124
Daniel Lidström wrote:
Hello!

I have a strange problem with my pimpl implementation. What happens is
that const correctness is not respected. Let me go straight to the code:

[snip]

class LineData
{
class LineDataImpl;
LineDataImpl& mImpl;
That's because your mImpl is not const.
[snip]
const Geometry& LineData::GetGeometry() const
{
return mImpl.GetGeometry();
Change this to:
return static_cast<const LineDataImpl&>(mImpl).GetGeometry();

Tom
Oct 14 '08 #2
Daniel Lidström wrote:
LineData::LineData()
: mImpl(*(new LineDataImpl))
{ }
Also note that this new leaks. I assume this is a test only, but your
real code should arrangements to delete this pointer.

Tom
Oct 14 '08 #3
"Tamas Demjen" <td*****@yahoo.comwrote
Daniel Lidström wrote:
>LineData::LineData()
: mImpl(*(new LineDataImpl))
{ }

Also note that this new leaks. I assume this is a test only, but your real
code should arrangements to delete this pointer.
Yes, the real code does delete the pointer :-)
Thanks for the workaround Tamas!

--
Daniel

Oct 15 '08 #4

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

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
6
by: Simon Elliott | last post by:
(Note: this is a real C++ question and not an MS Windows question. The window handle I introduce is just an example of OS specific data, which I've used to illustrate my question.) I have some...
3
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
2
by: Jim Strathmeyer | last post by:
I have a weird question about const correctness when using an stl list. I have a wrapper Inventory class that holds a list of pointers to Items. (Yes, they have to be pointers.) Now, obviously...
10
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
7
by: mliptak | last post by:
Hi group, suppose having this code using PIMPL: // a.hh class A { struct Impl; public: A();
4
by: er | last post by:
hi, the code below generates the following behavior. cld someone please help understand it? 1) clean project + build project generates build errors (see bottom) 2) build a second time, errors...
14
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.