943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 511
  • C++ RSS
May 27th, 2009
0

C++ style questions

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 27th, 2009
0

Re: C++ style questions

Quote ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 27th, 2009
0

Re: C++ style questions

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

CPP Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 27th, 2009
0

Re: C++ style questions

Click to Expand / Collapse  Quote originally posted by mostermand ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 27th, 2009
0

Re: C++ style questions

Click to Expand / Collapse  Quote originally posted by mostermand ...
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
c++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 70
Solved Threads: 4
Light Poster
DarthPJB is offline Offline
47 posts
since May 2009
May 27th, 2009
0

Re: C++ style questions

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 27th, 2009
0

Re: C++ style questions

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
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 27th, 2009
0

Re: C++ style questions

Click to Expand / Collapse  Quote originally posted by mostermand ...
Thank you for answering the questions
I'll leave the 'no problem' to Tux >_>;

Click to Expand / Collapse  Quote originally posted by tux4life ...
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.
Reputation Points: 70
Solved Threads: 4
Light Poster
DarthPJB is offline Offline
47 posts
since May 2009
May 27th, 2009
0

Re: C++ style questions

>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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 27th, 2009
0

Re: C++ style questions

Well come to think about it I don't know why I wanted to do it.
Maybe I am just tired.
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to read ascci from a file and convert it to string
Next Thread in C++ Forum Timeline: Logic error in my code.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC