Need a value from a class object

Get it from constructor or separate function?

Page 1 of 1

14 Replies - 574 Views - Last Post: 31 August 2009 - 03:05 PM Rate Topic: -----

#1 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Need a value from a class object

Posted 31 August 2009 - 12:32 PM

Okay, I've been posting a lot of questions this past week, but I think this might just be the last one (finally). I'm am writing a text based game. In this project have a class Game; when I create an instance of this class I need to get a string from it which will represent the name of the first location in the game.

Now, there are two ways I can do this: by passing the string by reference as a parameter in the class' constructor, or by having a separate function which will return the value of the string. Personally I'd go with the first option, because by using another function I'd make the class a little more difficult to use, not to mention that you could more easily screw it up. What do you think?

This post has been edited by diego_pmc: 31 August 2009 - 12:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need a value from a class object

#2 aks29921  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 290
  • Joined: 24-August 09

Re: Need a value from a class object

Posted 31 August 2009 - 12:39 PM

go for the constructor option
Was This Post Helpful? 0
  • +
  • -

#3 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Need a value from a class object

Posted 31 August 2009 - 01:10 PM

Yes use constructor.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need a value from a class object

Posted 31 August 2009 - 01:24 PM

...WHAT!?! Did any of you take a class in OOP?

encapsulation -- an object should control its own data. If you have a constructor such as:

class UnSafeData {
private:
string *myString;
public:
UnSafeData (string *data) : myString(data) { }
};


you just shot encapsulation all to hell. Adding getters and setters does not add all that much overhead to the class -- many IDE's will actually generate the code for you -- that is how common they are -- why are they that common? because you should control access to a classes private data!

If you don't care about encapsulation just make it a public field -- otherwise spend the 2 seconds it takes to add the extra "complexity" to your class.

-- I am just surprised to see two thumbs up for the constructor idea. Constructors are for initializing data -- that is what they are used for, don't try to go an make them do things they are not designed to do.
Was This Post Helpful? 0
  • +
  • -

#5 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Need a value from a class object

Posted 31 August 2009 - 01:54 PM

It's not exactly like that, there is no private string variable in Game. Here's a sketch of what I have:
class Game : public Player // Player holds info such as HP, 
                           // inventory, etc
{
public:
   string ProcessSection(string); 
   // ProcessSection() is the only public function; 
   // after it finished processing the location, dialog, 
   // or battle, it will return the name of the next location

private:
   // functions used by ProcessSection();
};

int main()
{
   string section;
   Game g;
   section = g.ProcessSection(section);
}


When processing the section, the player's status might change: more/less items in the inventory, more/less HP, etc. Also, there is no actual game data in the source code all game data is read from external text files.

Only class Game has acces to the script text file, so somehow I have to get the name of the first section from Game, before I can actually use the ProcessSection() function.

This post has been edited by diego_pmc: 31 August 2009 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Need a value from a class object

Posted 31 August 2009 - 02:13 PM

View PostNickDMax, on 31 Aug, 2009 - 01:24 PM, said:

--snip--


If that was the case, we'd never use anything but the default constructor. I use parameters in the constructor to initialize the values if that is what is needed. I also use getter and setters for any manipulation after the object is initially created (which is what I think your point was).

I'm a little confused are you against this?

class Cat {
private:
	int itsAge;
public:
	Cat()						  {itsAge = 5;};
	Cat(int age)			 {itsAge = age;}; //this is bad practice? 
	~Cat()				 {};
	int getAge()			  {return itsAge;};
};
#endif


This post has been edited by KYA: 31 August 2009 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#7 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Need a value from a class object

Posted 31 August 2009 - 02:25 PM

Hey what i said is
class Myclass
{
public:
		string Name;
		Myclass(){} //Default
		Myclass(string &s) //Overloaded
		{
			Name = s;
		}
} 
}


Am I wrong?I think this could be done in OOP and this doesnt shots encapsulation to hell.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Need a value from a class object

Posted 31 August 2009 - 02:32 PM

There's no reason to have public variables. It breaks OOP. That example could result in high coupling and low cohesion. Objects should access information only through certain interfaces (accessors/setters, etc...)
Was This Post Helpful? 0
  • +
  • -

#9 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Need a value from a class object

Posted 31 August 2009 - 02:35 PM

Maybe I didn't make it very clear. Which one of these should I use:
Passed by ref in constructor[/b]]class Game
{
public:
   Game(string& section) {
      section = GetStartSection(); // a private class
   }
};

int main()
{
   string section;
   Game g(section); // section will receive the value from GetStartSection()
}


... or...
Returned from a public function[/b]]class Game
{
public:
   string GetStartSection();
}

int main()
{
   string section;
   Game g;
   section = g.GetStartSection();
}


Which one should I use?

This post has been edited by diego_pmc: 31 August 2009 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need a value from a class object

Posted 31 August 2009 - 02:35 PM

no... I am "against" violating encapsulation by just passing a pointer to some data so that you can access it from both inside and outside the class (unless it is constant in which case there is no real violation of encapsulation).

So for example in KYA;s snippet it would be like a private member that was a pointer to an integer just so that you did not have to write a getter for the number later.

@diego_pmc -- So perhaps I misunderstood the topic (happens to me from time to time). So I suppose you are talking about doing something like this:
class GameFoo {
public:
GameFoo(string &willInitializeThisString)  { willInitializeThisString = "Got some string from the class"; }
};


Again I would say that this is a bad idea because that is not what constructors are for... but it works. I don't see why you could not overload the function so that if no string was passed in then it returned a string representing the current state (rather than any kind of change).

Just my 2 cents though. Constructors are for initializing the class -- not returning values.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Need a value from a class object

Posted 31 August 2009 - 02:37 PM

View PostNickDMax, on 31 Aug, 2009 - 02:35 PM, said:

Just my 2 cents though. Constructors are for initializing the class -- not returning values.


Then I misunderstood what you were saying (and perhaps the whole topic?). Returning anything from the constructor is bad practice. Initializing private data members is not.



edit: That example is backwards, why would you pass in data to set its value? Ugh. I completely missed what this was about. Setting a data member to the parameter is fine, but not the other way around. OK, I'm all clear now. :lol:

This post has been edited by KYA: 31 August 2009 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#12 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Need a value from a class object

Posted 31 August 2009 - 02:43 PM

So I should return it from a function, then.
Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need a value from a class object

Posted 31 August 2009 - 02:50 PM

you can use the same function name... just ditch the parameter.

class Game : public Player // Player holds info such as HP, 
						   // inventory, etc
{
public:
   string ProcessSection(string); //performs an operation
   string ProcessSection(); // return the current location (last value of ProcessSection(string) )
   // ProcessSection() is the only public function; 
   // after it finished processing the location, dialog, 
   // or battle, it will return the name of the next location

private:
   // functions used by ProcessSection();
};

int main()
{
   string section;
   Game g;
   section = g.ProcessSection(); //get the current state --- since ProcessSection has not been called before it is the initial value.
}


This way you don't really muddle the interface with a function that only has a function at 1 particular time in the lifecycle of the object.
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Need a value from a class object

Posted 31 August 2009 - 03:03 PM

Although accessors are typically named getDataName():

string getSection() {return section;};




Maybe that's just my thing though. 2c

This post has been edited by KYA: 31 August 2009 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need a value from a class object

Posted 31 August 2009 - 03:05 PM

actually it would probably be better to just make a separate function and give it a meaningful name. :) like KYA says.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1