?
Solved

pimpl + namespaces

Posted on 2010-09-05
5
Medium Priority
?
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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