class C_Class
{
	public:
		struct _Get
		{
                            void foo( void )
                            {
                                   m_Var = 0;
                            }
		}Get;
		
		struct _Set
		{
			
		}Set;
		
		long m_Var;
};

I like to organize my functions with get and set structures

Get.ThisThing();
Set.ThisThing();

But my structure's functions cant access the classes members unless I make a instance right off the bat and hard code it in.

Is there a way around this

Make Get static.

I dont see how that will help, also your page example doesn't load.

I'm missing something here. Can you post a compilable example?

#include <iostream>

using namespace std;

class C_User
{
	public:
		struct _Get
		{
			long	Number( void );
		}Get;
		
		struct _Set
		{
			void	Number( void );
		}Set;
		
		long m_iNumber;
};

long C_User::_Get::Number( void )
{
	return m_iNumber;
}

void C_User::_Set::Number( void )
{
	cout << "Enter a number: ";
	cin >> m_iNumber;
}

int main( int argc, char* args[] )
{
	C_User User;
	
	User.Set.Number();
	
	cout << "You entered: " << User.Get.Number() << endl;
	
	return EXIT_SUCCESS;
}

the structures do not have access to the class's members even though its created inside of the class.

The only way ive been able to use this is by Creating a instance of the class with the class delcaration.

And having it preset inside

void C_User::_Set::Number( void )
{
	cout << "Enter a number: ";
	cin >> User.m_iNumber;
}

This is very inconvenient.

--------------------------------------------------------------------------

Also another question to avoid a separate thread o:

How can I change my application's details. I could not find any information on it on msdn.

Example:
http://i41.tinypic.com/3589sig.png

Edited 6 Years Ago by VilePlecenta: n/a

Make Get static.

Making them static wont help as the data being accessed is not static so wont be there unless an instance of the class has been created.

In my opinion you are making things overly complex and attempting to introducing a style that is likely to just confuse anyone else trying to use the code you write which ultimately results in more maintenance overhead for no actual gain in functionality over

#include <iostream>

using namespace std;

class C_User
{
	public:
		long	getNumber( void );
		void	setNumber( void );

	private:
		long m_iNumber;
};

long C_User::getNumber( void )
{
	return m_iNumber;
}

void C_User::setNumber( void )
{
	cout << "Enter a number: ";
	cin >> m_iNumber;
}

int main( int argc, char* args[] )
{
	C_User User;
	
	User.setNumber();
	
	cout << "You entered: " << User.getNumber() << endl;
	
	return EXIT_SUCCESS;
}

If you wanted to do this you would have to do something even more obfuscating like store a reference to the parent object in _Get and _Set and initialise them in the _Get and _Set constructors when a C_User object is constructed and use the reference to access the parent object data inside the methods of _Get and _Set.

It makes sense that you can not directly access the parent object data inside _Get and _Set methods because inside those methods this refers to _Get and _Set instances respectively and not the parent class instance.

Edited 6 Years Ago by Banfa: n/a

I'm not sure I understand the purpose of your design here. You seem to be attempting to provide a class to ease user interaction yes? But this class also has the resposibility of storing and managing the data input? I feel there should be some separation of responsibility here with a class handling input and handing off data entered to other classes which may be more generally useful in the rest of your program.

Edited 6 Years Ago by mattjbond: n/a

If you wanted to do this you would have to do something even more obfuscating like store a reference to the parent object in _Get and _Set and initialise them in the _Get and _Set constructors when a C_User object is constructed and the reference to access the parent object data inside _Get and _Set.

It makes sense that you can not directly access the parent object data in _Get and _Set because inside the methods of those structures this refers to _Get and _Set respectively and not the parent class.

Agreed. Was just fiddling around with that but cannot get it to work in any useful manner. I tend to feel that if you continue down this path you will end up with a bunch of specialized classes that or not generally useful.

Its a very simple structure that helps instead of naming everything with Get or set, and it can limit members I make in the structures to those structures. Like making a class inside of a class, If its not possible then just let me know. If you don't like the design that is a matter of taste and not really a help answer, But im sure sub classing is very useful and should be used more often if it is not.

Ummm. But you have to provide Get and Sets for everything anyway... or am I missing something?

Comment on design was simply to point out that it is generally considered a good idea to restrict the reposnsibility of a class to a single well defined purpose. This is know as the single responsibility principle and it states that there should only ever be one reason for a class to change. By giving your class multiple responsibilities you contravene this rule of thumb. Your class would need to change if there were changes to the user interaction, or if the underlying data representations changed. If a class has more than one responsibility then these become coupled. A change to one may inhibit the classes ability to meet the other responsibility.
It is generally a good idea to keep coupling to a minimum.

check out this link for some articles on the above which may interest you...

http://www.objectmentor.com/omSolutions/oops_what.html

Sounds like a reasonable idea and I will be reading this literature since it seems helpful. But for now this is not a satisfying answer.

Rather than turning this into a a discussion of programming ethics.
I would like to know the answer to a simple question.

Can a structure or class inside of a class access the "Parent's" members.
If possible without creating a instance, how.

If its not possible then just let me know. If you don't like the design that is a matter of taste and not really a help answer, But im sure sub classing is very useful and should be used more often if it is not.

Like I said, it is possible, your _get and _set structures need to have a constructor that takes and stores a reference to the parent object.

Actually not liking a design is not only "a matter taste". Some designs are just plan wrong and or foolish. That said there is certainly more than 1 good design to solve any problem.

You may think sub classing is very useful I on the other hand think you are creating a confusing and obfuscated mess.

However you may wish to look at the Bridge design pattern which does something a little similar.

This article has been dead for over six months. Start a new discussion instead.