how to change the value of an object ?

A

asdf

May I change the value of data members of an object by using the
constructor?

struct A
{
int x;
default y;
}

int main()
{
A aobj;
}

aobj is an object of the class A, and I want to change the values of x
and y of the aobj, how can I do? Thanks.
 
G

Gianni Mariani

asdf said:
May I change the value of data members of an object by using the
constructor?

struct A
{
int x;
default y;
}

int main()
{
A aobj;

aobj.x = 1;
aobj.y = aobj.y;
 
K

Kai-Uwe Bux

asdf said:
May I change the value of data members of an object by using the
constructor?

struct A
{
int x;
default y;
}

int main()
{
A aobj;
}

aobj is an object of the class A, and I want to change the values of x
and y of the aobj, how can I do?

In your case, e.g.:

aobj.x = 5;
aobj.y = 7 - aobj.y + aobj.x;


Best

Kai-Uwe Bux
 
O

Old Wolf

Kai-Uwe Bux said:
In your case, e.g.:

aobj.x = 5;
aobj.y = 7 - aobj.y + aobj.x;

I'm not familiar with this "default y;" syntax, and my compiler
doesn't accept it .. can you fill me in?
 
M

Mike Wahler

asdf said:
May I change the value of data members of an object by using the
constructor?

No. A constructor is only invoked at creation time.
It is used to initialize an object.

struct A
{
int x;
default y;

'default' is a reserved word in C++. You can't use
it as a type or a name.
}

int main()
{
A aobj;
}

aobj is an object of the class A, and I want to change the values of x
and y of the aobj, how can I do? Thanks.


struct A
{
int x;
int y;
A(int x_parm, int y_parm) : x(x_parm), y(y_parm) /* constructor */
{
}

int main()
{
A obj(1, 2); /* creates a type 'A' object whose member 'x'
is initialized to 1, and whose member 'y'
is initialized to 2 */

obj.x = 42; /* changes member 'x' */
obj.y = 99; /* changes member 'y' */

return 0;
}

-Mike
 
K

Kai-Uwe Bux

Old said:
I'm not familiar with this "default y;" syntax, and my compiler
doesn't accept it .. can you fill me in?

Nope. Maybe the OP can.


Best

Kai-Uwe Bux
 
D

David Harmon

On 3 Dec 2006 12:55:36 -0800 in comp.lang.c++, "Old Wolf"
I'm not familiar with this "default y;" syntax, and my compiler
doesn't accept it .. can you fill me in?

The 'default' keyword is not legal in that context at all. One can only
assume that the code posting got mangled by a Microsoft spellchecker.
 
J

Jim Langston

asdf said:
May I change the value of data members of an object by using the
constructor?

struct A
{
int x;
default y;
I presume this is supposed to be
int y;
}

int main()
{
A aobj;
}

aobj is an object of the class A, and I want to change the values of x
and y of the aobj, how can I do? Thanks.

It's called the dot operator.

aobj.x
If aobj is a pointer then you use the arrow operator:
A* aobj = new A;
aobj->x
although you can also dereference the pointer and use the dot operator
(*aobj).x
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top