?
Solved

pimpl + namespaces

Posted on 2010-09-05
5
Medium Priority
?
394 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 2000 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

850 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