Partial Class Definition and *&

Sep 4, 2009 at 1:08pm
1. Let's say there is a class - that is part of a library - intended for the programmer's use. But if I give the programmer a header file with the full definition, he can easily change private members to public. Can I let the programmer use a partial definition that contains only the public members?

2. Can I use a reference to a pointer? If yes, is it int*& ptrref; or int&* ptrref ?
Last edited on Sep 4, 2009 at 1:10pm
Sep 4, 2009 at 1:14pm
1. No. And no, I'm pretty sure the user can't do that.

2. Yes. It's the former. The latter is a pointer to a reference, which is impossible.
Sep 4, 2009 at 1:18pm
But the user could do that. Nothing about the mangled names of the variables include the access type of the variable. Having said that, I would not recommend a user do such
a thing. But if you really, really don't want the user to see the details of your class, the
solution is to use the pImpl idiom. (pointer to implementation). Search online for pImpl.
A simple pImpl is:
Sep 4, 2009 at 1:42pm
Yeah a proxy class would work.
Sep 4, 2009 at 1:52pm
A simple pImpl is:
Um...
Sep 4, 2009 at 3:15pm
I'm not familiar with the term pImpl, sounds like a proxy class to me which would be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Implementation.h
// Implementation class definition.

class Implementation
{
public:
	// constructor
	Implementation( int v ): value( v ) 
	{ 
	}

	// set value to v
	void setValue( int v )
	{
		value = v;
	}

	// return value
	int getValue() const
	{
		return value;
	}

private:
	int value; // datat that we would like to hide from the client
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Interface.h
// Proxy Class interface definition
// Client sees this source code, but the source code does not reveal the data layout of class Implementation

class Implementation; // forward declaration

class Interface
{
public:
	Interface( int );
	void setValue( int );
	int getValue() const;
	~Interface();

private:
	Implementation *ptr;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Interface.cpp
// Implementation of class Interface -- client receives this file only as precompiled object code, keeping 
// the implementation hidden.
#include "Interface.h"
#include "Implementation.h"

// constructor
Interface::Interface( int v ) : ptr ( new Implementation( v ) )
{
}

void Interface::setValue( int v )
{
	ptr->setValue( v );
}

int Interface::getValue() const
{
	return ptr->getValue();
}

Interface::~Interface()
{
	delete ptr;
}
Last edited on Sep 4, 2009 at 3:16pm
Sep 4, 2009 at 4:11pm
yes, that be a pimpl.

sorry I was going to write one, but ran out of time and forgot to delete the line.
Sep 4, 2009 at 6:53pm
Thanks for your help! I don't think I need a pimpl...I just wanted to know what the standard method is. I'm working on an open-source project anyway so I don't really need to hide anything.
Sep 4, 2009 at 10:08pm
What's:
Can I use a reference to a pointer? If yes, is it int*& ptrref; or int&* ptrref ?
did you mean:
int** ptrref;???
you need a pointer to a pointer right?Or I didn't get it right?
Sep 4, 2009 at 10:32pm
You didn't get it right.
Topic archived. No new replies allowed.