Solved

pimpl + namespaces

Posted on 2010-09-05
5
380 Views
Last Modified: 2012-06-22
Hi,

I have problem with pimpl idiom used with namespaces

 
--file1--
namespace space_a {
namespace implementation {

class Impl
{
 public:
 Impl();
 void test();
};

}}


--file2--
#include file1

namespace space_a {

class A
{
 public:
 A(); 
 private:
 // here impl declaration *pimpl_;
 // without namespaces i could do
 // class Impl* pimpl_; 
};

A::A()
{
 pimpl_->test();
}

}

Open in new window


I have tried this also but i get unresolved error
 
--file1--
namespace space_a {
namespace implementation {

class Impl
{
 public:
 Impl();
 void test();
};

}}


--file2--
#include file1

namespace space_a {

// new
namespace implementation {
  class Impl;
}

class A
{
 public:
 A(); 
 private:
 //new
 implementation::Impl* pimpl_;
};

A::A()
{
 pimpl_->test();
}

}

Open in new window


All is ok if class A is not in namespace
0
Comment
Question by:lavitz
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
You've not provided any implementation for the pimp test function.
0
 

Author Comment

by:lavitz
Comment Utility
I do. Its example. In my project there is implementation.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
Comment Utility
>> Its example. In my project there is implementation.
Well, that is the only thing wrong with your example, fix that and it builds fine.


//--file1--

namespace space_a {

namespace implementation {



class Impl

{

 public:

 Impl();

 void test(){}

};



}}





//--file2--

//#include file1



namespace space_a {



// new

namespace implementation {

  class Impl;

}



class A

{

 public:

 A(); 

 private:

 //new

 implementation::Impl* pimpl_;

};



A::A()

{

 pimpl_->test();

}



}



int main()

{

	space_a::A a;

}

Open in new window

0
 

Author Comment

by:lavitz
Comment Utility
Hmm i see. So i have typo in my code.

I ask one additional question.
It is good?  to name class impl and class using that impl same name? and differ them by namespace?
So i could have:


namespace pipe_service {
namespace implementation {

class PipeService { // pimpl class

}

}}

and 

namespace pipe_service {

class PipeService { // class that use pimpl

}

}

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 500 total points
Comment Utility
>> It is good?  to name class impl and class using that impl same name? and differ them by namespace?
I would probably not do that since it could cause you issues with ADL and make the implementation more convoluted than it needs to be.
http://en.wikipedia.org/wiki/Argument-dependent_name_lookup

I would stick to putting them in the same namespace and call them PipeService and PipeServiceImpl
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now