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?

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?

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

int main()
{
    class mainClass
    {
        mainClass()
        {
            //initialization and run
        }
        ~mainClass()
        {
            //clean up
        }
    } Main;
}

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 :)

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

function(int number, char* Astring)
{
char* bob;
bob = new char[number]
strcpy(bob, Astring);
DoSomething(bob);
delete bob;
}

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.

[B]bob = *Astring;[/B] won't copy Astring in bob, for that purpose you'll have to use the following code instead: strcpy(bob, Astring); :P

>However there is no real *right* way of doing it
But there are good and bad ways to do it :)

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

Thank you for answering the questions

I'll leave the 'no problem' to Tux >_>;

[B]bob = *Astring;[/B] won't copy Astring in bob, for that purpose you'll have to use the following code instead: strcpy(bob, Astring); :P

>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!

>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 :P

Well come to think about it I don't know why I wanted to do it.
Maybe I am just tired.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.