#### Archived

This topic is now archived and is closed to further replies.

This topic is 5028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if i have a pointer that is a private member of a class, and i send this pointer to a function where its value changes, is this bad? also, i can assign an object the value of another object, such as blah A; blah A; A.whatever = 2; B = A; this will copy all the values that A has and put them into B? is this how it works? what if some values arent assigned yet, does it just get garbage? also, i have seen this used and its been bothering me, ive seen people do this with a class: classname instance = {value,anothervalue,4,etc}; how does this work? does it start from the top of the class, and go down? like if i had class blah { int a; string b; float c; } blah myblah = {10,"hello", 4.2}; is this how it works? thanks for any help!! BTW im using c++ with .net compiler Donkey Punch Productions.net

##### Share on other sites
quote:
Original post by graveyard filla
if i have a pointer that is a private member of a class, and i send this pointer to a function where its value changes, is this bad?

also, i can assign an object the value of another object, such as

blah A;
blah A;

A.whatever = 2;

B = A;

Yes. This is done by the assignment operator (operator=()); one that performs memberwise copies is defined by default.
quote:

this will copy all the values that A has and put them into B? is this how it works? what if some values arent assigned yet, does it just get garbage?

It copies member by member. However, an object should never contain garbage; the class constructor should ensure that the object is initialised to a valid state.
quote:

also, i have seen this used and its been bothering me, ive seen people do this with a class:

classname instance = {value,anothervalue,4,etc};

how does this work? does it start from the top of the class, and go down? like if i had

class blah
{
int a;
string b;
float c;
}

blah myblah = {10,"hello", 4.2};

is this how it works?

Yes. I don''t believe this is allowed with general classes; however, it is allowed in C (I believe this construct is referred to as an array initialiser), and so it is also allowed for C++ POD classes (Plain Old Data - that is, a class that has member data but no member functions).
quote:

thanks for any help!!

You''re welcome.

##### Share on other sites
It''s usually good to use points or references to objects inside a function. If you don''t pass them as pointer/reference, the copy constructor will get called, thus costing extra CPU cycles.

Generally, it''s good to ALWAYS use pointers/references for functions. For example, the following function takes an object, just to use some data for it. But it doesn''t change any data:

void FunctionX(const CMyClass *pClass){    int X = pClass->X();    int Y = pClass->Y();    Z = X * Y;    cout << Z << endl;}

This saves you a pass from the copy constructor, and the const keyword makes sure you can''t change the pClass object. The same goes with references. If you need to pass an object, ALWAYS do this by pointer of reference, unless you want to copy the object.

Toolmaker

My site
/* -Earth is 98% full. Please delete anybody you can.*/

##### Share on other sites
quote:
blah myblah = {10,"hello", 4.2};

Also works with non POD-classes, just think about a custom copyoperator:

void blah::operator= (struct {int i;std::string blub;float F;} data){   //set values in the class to the values in data};