C++ style questions

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

C++ style questions

 
0
  #1
May 27th, 2009
I have been testing a C++ based SDK and now i have a few questions for you that i hope you will answer
.

1. When passing objects between each other is it preffered to use references if not what then?

2. Is it good or bad to make a single object in main that initializes runs and cleans up?

3. Would that be the same as a singleton class without multiple instance protection and would the usual arguments for and against them apply?

4. When testing the SDK i found that member variables of classes was prefixed a m_ what does that mean and why would you do it?

5. When allocating resources should you always do it inside of a class?

Thank you for your time
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: C++ style questions

 
0
  #2
May 27th, 2009
1. When passing objects between each other is it preffered to use references if not what then?

5. When allocating resources should you always do it inside of a class?
To answer your first question: yes, the preferred way of passing objects is pass by reference

In your fifth question you make an assumption which is wrong (unless you implement a destructor for your class as well), if you use dynamic memory allocation inside a class, and you don't make use of a destructor, the memory won't be freed

And regarding question 4, what SDK are you talking about? It's common in class libraries that there are rules to name for example variables or functions, i.e. to make it easier for the programmer to go through/read the code

Could you be more specific in your second question?
Last edited by tux4life; May 27th, 2009 at 3:05 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: C++ style questions

 
0
  #3
May 27th, 2009
Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?

As for question number two here is an example of what I mean

  1. int main()
  2. {
  3. class mainClass
  4. {
  5. mainClass()
  6. {
  7. //initialization and run
  8. }
  9. ~mainClass()
  10. {
  11. //clean up
  12. }
  13. } Main;
  14. }
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: C++ style questions

 
0
  #4
May 27th, 2009
Originally Posted by mostermand View Post
Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?
If you mean all resources (also variables which aren't on the heap) , then I would answer no, in a class you put elements and functions related to each other.

However, it seems like you're pulling in the direction of a garbage collector.
If you like this concept, then it would be probably better to Google on "C++ Garbage Collector" or something
Last edited by tux4life; May 27th, 2009 at 3:24 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 45
Reputation: DarthPJB is on a distinguished road 
Solved Threads: 4
DarthPJB's Avatar
DarthPJB DarthPJB is offline Offline
Light Poster

Re: C++ style questions

 
0
  #5
May 27th, 2009
Originally Posted by mostermand View Post
Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?
It really depends on what kind of resources your allocating and why. if for example you are creating a new string to hold a line of text during the run of a single function there would be little reason to use an entire class.
eg
  1. function(int number, char* Astring)
  2. {
  3. char* bob;
  4. bob = new char[number]
  5. strcpy(bob, Astring);
  6. DoSomething(bob);
  7. delete bob;
  8. }
if however you had a large amount of data being loaded from some kind of custom database that needed to be shared around the entire program then using a class would be preferable.

However there is no real *right* way of doing it, as long as you clean up any memory you allocate and your program completes it's purpose effectively all it well. Though I have little doubt others have different viewpoints on the matter.
Last edited by DarthPJB; May 27th, 2009 at 3:28 pm. Reason: Tux pointed out strcpy(bob, Astring); silly old me!
code tags, there to give us line numbers and colours! [code] is bad, [code=C++] is good!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: C++ style questions

 
0
  #6
May 27th, 2009
bob = *Astring; won't copy Astring in bob, for that purpose you'll have to use the following code instead: strcpy(bob, Astring);

>However there is no real *right* way of doing it
But there are good and bad ways to do it
Last edited by tux4life; May 27th, 2009 at 3:28 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: C++ style questions

 
0
  #7
May 27th, 2009
Thank you for answering the questions I will just assume it is ok to make a main class.

I didn't think non heap variables could be said to be allocated because they are allocated by the OS at startuptime
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 45
Reputation: DarthPJB is on a distinguished road 
Solved Threads: 4
DarthPJB's Avatar
DarthPJB DarthPJB is offline Offline
Light Poster

Re: C++ style questions

 
0
  #8
May 27th, 2009
Originally Posted by mostermand View Post
Thank you for answering the questions
I'll leave the 'no problem' to Tux >_>;

Originally Posted by tux4life View Post
bob = *Astring; won't copy Astring in bob, for that purpose you'll have to use the following code instead: strcpy(bob, Astring);

>However there is no real *right* way of doing it
But there are good and bad ways to do it
A very valid point, my mistake, though the example still stands and I shall amend my post!
Last edited by DarthPJB; May 27th, 2009 at 3:30 pm.
code tags, there to give us line numbers and colours! [code] is bad, [code=C++] is good!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: C++ style questions

 
0
  #9
May 27th, 2009
>I didn't think non heap variables could be said to be allocated because they are allocated by the OS at startuptime
Not necessarily, they're automatically allocated when the code block in which they're defined is entered/executed

BTW, what would be the advantage of wrapping all the variables of your program in one class? (what advantages do you see in it?) I don't see any advantage in it, I find it sluggish and a very bad coding practice as well ...

Remember: It's not because you can do something that you'll have to do it as well
Last edited by tux4life; May 27th, 2009 at 3:45 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: C++ style questions

 
0
  #10
May 27th, 2009
Well come to think about it I don't know why I wanted to do it.
Maybe I am just tired.
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 285 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC