Solved

pimpl + namespaces

Posted on 2010-09-05
5
382 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
ID: 33606103
You've not provided any implementation for the pimp test function.
0
 

Author Comment

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

Accepted Solution

by:
evilrix earned 500 total points
ID: 33606165
>> 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
ID: 33606191
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
ID: 33606226
>> 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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