SRL Archive - C/C++ Help and Tutorials

Destrcuters


HarryJames

2011-05-20 22:35:56

Is a class object automatically destructed, or only if you actually assign it something?

So if I don't declare the destructer and do a player::~player, will it just stay in memory?

If this doesn't make sense - Tell me, I'm tired. G'night!

Cool Aide

2011-05-20 23:07:25

Yes.

No.

i luffs yeww

2011-05-20 23:29:35

You should read moar about how stuff works before coding stuff. ;)

Don't have much to say about this though. I'm pretty positive I could help you, but I also don't wanna give you wrong information.

But, talk to me if you need help! :p I could probably help.

HarryJames

2011-05-21 06:40:50

Cool Aide understood what I put :3

I've just started learning classes in C++ (i've not done C++ for roughly a year, so I need to get back into it).

I'll post my source code, just so you get a view of what I was on about!
#include <iostream>
#include <string>
using namespace std;

class player
{
public:
player (); // Constructer
~player (); // Destructer

// VARIABLES THAT EACH OBJECT HAS
std::string name;
int age;

// FUNCTIONS THAT EACH OBJECT HAS
void print() const;
std::string getName() const;
};

// The constructer and destructer
player::player() { cout << "Object has been made" << endl; name="No Name"; age = 5;};
player::~player( ) {cout << "The object " << name << " has been destructed" << endl; cin.get(); };

// The functions and what they do
string player::getName() const { return name; }
void player::print() const { cout << "Name: " << name << endl << "Age: " << age << endl << endl; }

int main()
{
player our_Player;
//our_Player.name = "Harry";
cout << our_Player.name << endl;
cout << "What's your name: ";
cin >> our_Player.name;
// cout << our_Player.getName();
our_Player.print();
cin.get();
}

superuser

2011-05-21 07:00:20

From: HarryJames
Cool Aide understood what I put :3

I've just started learning classes in C++ (i've not done C++ for roughly a year, so I need to get back into it).

I'll post my source code, just so you get a view of what I was on about!
#include <iostream>
#include <string>
using namespace std;

class player
{
public:
player (); // Constructer
~player (); // Destructer

// VARIABLES THAT EACH OBJECT HAS
std::string name;
int age;

// FUNCTIONS THAT EACH OBJECT HAS
void print() const;
std::string getName() const;
};

// The constructer and destructer
player::player() { cout << "Object has been made" << endl; name="No Name"; age = 5;};
player::~player( ) {cout << "The object " << name << " has been destructed" << endl; cin.get(); };

// The functions and what they do
string player::getName() const { return name; }
void player::print() const { cout << "Name: " << name << endl << "Age: " << age << endl << endl; }

int main()
{
player our_Player;
//our_Player.name = "Harry";
cout << our_Player.name << endl;
cout << "What's your name: ";
cin >> our_Player.name;
// cout << our_Player.getName();
our_Player.print();
cin.get();
}


There's a few issues with your code. Here's something more like it:

#include <iostream>
#include <string>

class Player
{
public:
Player()
: _age(5)
, _name("No Name")
{
std::cout << "The object " << _name << " has been created" << std::endl;
}

virtual ~Player()
{
std::cout << "The object " << _name << " has been destructed" << std::endl;
}

void Print() const
{
std::cout << "Name: " << _name << std::endl << "Age: " << _age << std::endl << std::endl;
}

// Remember the const correctness!
inline const std::string & GetName() const
{
return _name;
}

void SetName(const std::string & n)
{
_name = n;
}

private: // Keep in mind the visibility.
std::string _name;
int _age;
};

int main()
{
Player *player = new Player();

// You could also use smart pointers.. something like
// boost::shared_ptr<Player> player(new Player());

std::cout << player->GetName() << std::endl;

player->Print();

// And so on...

delete player;

// If using smart pointers, no need to delete.
}

Too lazy to comment the code more, ask if you need.

Shuttleu

2011-05-21 07:04:15

might i suggest you make the vars private then make functions to return and edit them

~shut

HarryJames

2011-05-21 07:11:03

Superuser, a lot of that went over my head.
Why do you define the variables with an underscore at the start, and what is an inline (and why do you declare const twice).
inline const std::string & GetName() const


Shuttleu, I was going to do that anyway but I didn't think of making them private. Thanks :)

Sabzi

2011-05-21 07:24:51

inline - You tell the compiler that you want to make that function inline which means the code itself will be pasted everywhere you use that function, so it won't call it. Makes program faster, useful for short functions.

first const means the returning string will be const(I think you know what that means)

second const won't allow the function to change variables inside it(so if you still want to, the compiler will tell that to you -> no headaches finding what you made wrong)

Underscore is just personal preference isn't it?

I very well may be mistaken so correct me if I'm wrong, but I am curious how well I know these things. :)

HarryJames

2011-05-21 07:44:05

I didn't know if the underscores had something to do with classes, but I presume it'll be preference. So I should use inline for everything that returns an object's variable (like getName etc)?

I've made some changes, and a few source files to have stuff more organised. I'll upload it now if anybody wants to see it. :)

Download me if you want!

Sabzi

2011-05-21 07:47:42

Nah, inline is good, but imo it shouldn't be overused.
Use it for simple functions like the GetSomethings though, sure.
I think those will be inline anyway, depending what compiler you use. Not sure.

HarryJames

2011-05-21 08:06:05

I use Mingw (Dev C++) :)

superuser

2011-05-21 08:58:45

From: HarryJames
Superuser, a lot of that went over my head.
Why do you define the variables with an underscore at the start, and what is an inline (and why do you declare const twice).


Shuttleu, I was going to do that anyway but I didn't think of making them private. Thanks :)


The underscore is just there to show me that I'm using variables from the class. Otherwise (depending on IDE), i might confuse them on local ones. inline -keyword is pretty much useless nowadays, but I added it there since I've used in for the last 20 years :)

inline is a hint to the modern compilers to inject the code as it is when it's called. Compiler might do this or not - as I said, it's just a hint. Even if you don't type inline there, the compiler might inline the function still. I guarantee you, micro-optimizations are irrelevant nowadays.

The first const from

const std::string & Getname() const

tells compiler to return a reference to the class' variable. Therefore, it doesn't return copy of it but a reference instead. When you compile your code in debug mode, it doesn't matter, but there is a difference in fully optimized code.

Generally speaking, you should hide your class variables from the user. The user should only need to know how to use the class, not the little nuances and implementation details.

I highly recommend you to read C++ faq lite. There's very good information for your disposal - including const correctness ;)