Jump to content

Calling struct from within a class.

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Atomic_Sheep

Atomic_Sheep

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, I created a struct within a class and then 'instatiated'? it with variables within the same class:
void Name::class()
{
		Variable sVariable;
		sVariable.strName = "text or for other stuct members these were int double etc taken from the UI";
}

How do I access these members from another class? For example, I want to use the struct variables for comparison with other variables in the other class? Since they are already instantiated, then it should be as simple as:
if(class.sVariable == 5)
{
	//do something
}

But I'm not sure what to write for the first part of the if statement.

#2
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
You need to make sVariable either a public( i dont usually do this ) or protected( if you planning on inheritence ) member of your class. but if you choose to make it private you can write a function that will return the value of a variable within the struct. so something like: string Name::getStrName( ){ return sVariable.strName; }
then you can say
		  if( class.getStrName( ) == "this is a string )
			   //do something

you can write get functions to return different types from the struct within your class

---------- Post added at 09:11 AM ---------- Previous post was at 09:11 AM ----------

WHAT!!! the code tag didnt work, i am sure i did it right

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You made an instance (instantized?) of that struct in a member function and you can't access it outside of that member function. You have to make it a member variable, like untitled_1 said.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
Atomic_Sheep

Atomic_Sheep

    Newbie

  • Members
  • Pip
  • 4 posts
I see, what about using pointers? I'm just not too keen on creating 15 functions for the variables that I need to access within the struct.

#5
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 282 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
I think if you follow the following way to manage your structure and class, your problem will get solved.

// your structure definition
typedef struct Variable {
std::string var1;
std::string var2;
int var3;
};


// your class that uses the structure
class Foo {
public:
Foo() {
_var.var1 = "";
_var.var2 = "";
_var.var3 = 0;
}


// this will let access a constant Variable of your _var to the user of this class
// so other will not be able to modify it.
const Variable& getVariable() {return _var;}

void method1()
{
// You can modify your _var here
_var.var1 = "First String";
}

void method2()
{
// You can modify your _var here
_var.var2 = "Second String";
}

void method3()
{
// You can modify your _var here
_var.var3 = 10;
}

private:
Variable _var;
};


// Using the Foo object to access the structure outside the class.
void UsingFoo()
{
Foo foo;

foo.method1();
if (foo.getVariable().var1 == "First String") {
cout << "var1 -- equal";
}

foo.method2();
if (foo.getVariable().var2 == "Second String") {
cout << "var2 -- equal";
}

foo.method3();
if (foo.getVariable().var3 == 10) {
cout << "var3 -- equal";
}

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users