| | |
C++ style questions
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 36
Reputation:
Solved Threads: 1
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
.
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
•
•
•
•
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?
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."
•
•
Join Date: Sep 2008
Posts: 36
Reputation:
Solved Threads: 1
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
As for question number two here is an example of what I mean
CPP Syntax (Toggle Plain Text)
int main() { class mainClass { mainClass() { //initialization and run } ~mainClass() { //clean up } } Main; }
All i've got is a slice of pi
•
•
•
•
Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?
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."
•
•
•
•
Yes I was assuming a destructor was implemented but my question still applies should I allocate all my resources in classes?
eg
c++ Syntax (Toggle Plain Text)
function(int number, char* Astring) { char* bob; bob = new char[number] strcpy(bob, Astring); DoSomething(bob); delete bob; }
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!
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."
I'll leave the 'no problem' to Tux >_>;
A very valid point, my mistake, though the example still stands and I shall amend my post!
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!
>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
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."
![]() |
Similar Threads
- AutoPostback Problem on the Grid (ASP.NET)
- Style properties are ignored by Mozilla browser... (ASP.NET)
- need help with vbulletin (Social Media and Online Communities)
- Main Options / Style Questions (C++)
Other Threads in the C++ Forum
- Previous Thread: How to read ascci from a file and convert it to string
- Next Thread: Logic error in my code.
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game generator getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






