473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

pointers and assignment operators, basic question

I am trying to remember how to code in C++ after many years of using
Java exclusively.

I have this setup:

class Base {
public:
virtual void printA(){}
};

class D1 : public Base {
public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}
};

class D2 : public Base {
public:
D2() {
a = 2;
}
int a;
void printA() {cout << a << endl;}
};

int main() {
D1 * d1 = new D1;
D2 * d2 = new D2;
Base * b1 = d1;
Base * b2 = d2;
*b1 = *b2; // HERE What does this _do_?
b1->printA();
return 0;
}

The program, as _you_ would expect, outputs 1. _I_ am trying to
figure out why it doesn't output 2.

What does the line marked HERE do? I expected it to overwrite the
memory that starts at b1 with the memory that starts at b2, but that
is clearly not the case or the output would be 2. If I do something
like this:

int * a = new int(3);
int * b = new int(5);
*a = *b;

the memory starting at a was overwritten by the contents of the memory
in b. Why is it different in the situation above? What am I
missing? Thanks for helping me to remember this stuff.

Jun 1 '07 #1
6 1193
Jeff Bender <ji***********@gmail.comwrote in
news:11**********************@g4g2000hsf.googlegro ups.com:
I am trying to remember how to code in C++ after many years of using
Java exclusively.

I have this setup:

class Base {
public:
virtual void printA(){}
};

class D1 : public Base {
public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}
};

class D2 : public Base {
public:
D2() {
a = 2;
}
int a;
void printA() {cout << a << endl;}
};

int main() {
D1 * d1 = new D1;
D2 * d2 = new D2;
Base * b1 = d1;
Base * b2 = d2;
*b1 = *b2; // HERE What does this _do_?
Nothing. It attempts to call Base::operator=() and passing it a Base& to
*b2, which by default does a memberwise assignment of the Base portion.
Since Base has no members, it does nothing.
b1->printA();
return 0;
}

The program, as _you_ would expect, outputs 1. _I_ am trying to
figure out why it doesn't output 2.

What does the line marked HERE do? I expected it to overwrite the
memory that starts at b1 with the memory that starts at b2, but that
is clearly not the case or the output would be 2. If I do something
like this:
Uh, no. It does memberwise assignment, not a memcpy.
int * a = new int(3);
int * b = new int(5);
*a = *b;

the memory starting at a was overwritten by the contents of the memory
in b. Why is it different in the situation above? What am I
missing? Thanks for helping me to remember this stuff.
No, this assigns an int to an int. There's a difference between:

*a = *b;

and

memcpy(a, b, sizeof(*a));

(Granted, not much different for an int, could be _wildly_ different for
a class.)
Jun 2 '07 #2
On Jun 2, 8:17 am, Jeff Bender <jigaboophe...@gmail.comwrote:
I am trying to remember how to code in C++ after many years of using
Java exclusively.

I have this setup:

class Base {
public:
virtual void printA(){}

};

class D1 : public Base {
public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}

};

class D2 : public Base {
public:
D2() {
a = 2;
}
int a;
void printA() {cout << a << endl;}

};

int main() {
D1 * d1 = new D1;
D2 * d2 = new D2;
Base * b1 = d1;
Base * b2 = d2;
*b1 = *b2; // HERE What does this _do_?
b1->printA();
return 0;

}

The program, as _you_ would expect, outputs 1. _I_ am trying to
figure out why it doesn't output 2.

What does the line marked HERE do? I expected it to overwrite the
memory that starts at b1 with the memory that starts at b2, but that
is clearly not the case or the output would be 2. If I do something
like this:

int * a = new int(3);
int * b = new int(5);
*a = *b;

the memory starting at a was overwritten by the contents of the memory
in b. Why is it different in the situation above? What am I
missing? Thanks for helping me to remember this stuff.
The assignment really invokes the operator= function generated by the
compiler, which does a bitwise copy.

Copy constructor, operator=, default constructor, destructor functions
will be generated by compiler if we do not write our own.

Regards.
Sarath

Jun 2 '07 #3
Jeff Bender wrote:
I am trying to remember how to code in C++ after many years of using
Java exclusively.

I have this setup:

class Base {
public:
virtual void printA(){}
};

class D1 : public Base {
public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}
};

class D2 : public Base {
public:
D2() {
a = 2;
}
int a;
void printA() {cout << a << endl;}
};

int main() {
D1 * d1 = new D1;
D2 * d2 = new D2;
Base * b1 = d1;
Base * b2 = d2;
*b1 = *b2; // HERE What does this _do_?
b1->printA();
return 0;
}

The program, as _you_ would expect, outputs 1. _I_ am trying to
figure out why it doesn't output 2.

What does the line marked HERE do? I expected it to overwrite the
memory that starts at b1 with the memory that starts at b2, but that
is clearly not the case or the output would be 2. If I do something
like this:

int * a = new int(3);
int * b = new int(5);
*a = *b;

the memory starting at a was overwritten by the contents of the memory
in b. Why is it different in the situation above? What am I
missing? Thanks for helping me to remember this stuff.
I think you are missing that operator= is not virtual. So because you
call it with Base object all you are doing is calling the Base::operator=.

john
Jun 2 '07 #4
On Jun 2, 3:17 am, Jeff Bender <jigaboophe...@gmail.comwrote:
I am trying to remember how to code in C++ after many years of using
Java exclusively.

I have this setup:

class Base {
public:
virtual void printA(){}

};

class D1 : public Base {
public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}

};

class D2 : public Base {
public:
D2() {
a = 2;
}
int a;
void printA() {cout << a << endl;}

};

int main() {
D1 * d1 = new D1;
D2 * d2 = new D2;
Base * b1 = d1;
Base * b2 = d2;
*b1 = *b2; // HERE What does this _do_?
b1->printA();
return 0;

}

The program, as _you_ would expect, outputs 1. _I_ am trying to
figure out why it doesn't output 2.

What does the line marked HERE do? I expected it to overwrite the
memory that starts at b1 with the memory that starts at b2, but that
is clearly not the case or the output would be 2. If I do something
like this:

int * a = new int(3);
int * b = new int(5);
*a = *b;

the memory starting at a was overwritten by the contents of the memory
in b. Why is it different in the situation above? What am I
missing? Thanks for helping me to remember this stuff.
assume:

int *a;
int *b;

remark1:

a=b;//this means that from now on 'a' points to where 'b' used to
point.
/*now a==b*/
remark2:

*a=1;//sets the value of the memmory location pointed to by 'a' to 1
*a=*b;/*sets the value of the memmory location pointed to by 'a' to
the value stored at the memmory location pointed to by 'b' .In general
this means that 'a!=b' but '*a==*b' for a while(unless you modify 'a'
or 'b') */
What does the line marked HERE do? I expected it to overwrite the
It does not care what 'b1' and 'b2' actually point to.It just copies a
'Base' object(not a 'D1' nor a 'D2') from the location pointed to by
'b2' to the location pointed to by 'b1' .
figure out why it doesn't output 2.

since the type of 'b1' is still considered to be 'Base' nothing is
printed to the output.
*b1 = *b2; // HERE What does this _do_?
try this one:

b1 = b2;//now the next line prints 2 to the output.
remark3:

the meaning of '*' in pointer delarations and pointer-cast operators
differs from the meaning of '*' in dereferencing statements.

regards,
FM
Jun 2 '07 #5

Jeff Bender <ji***********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
I am trying to remember how to code in C++ after many years of using
Java exclusively.
I have this setup:

class Base { public:
virtual void printA(){}
};

class D1 : public Base { public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}
};
You should prefer 'initializer lists' (vs. assignment) in constructors:

class D1 : public Base { public:
D1() : a( 1 ){} // note the colon
int a;
void printA(){ cout << a << endl;}
};

--
Bob R
POVrookie
Jun 2 '07 #6
Everyone, thanks a lot for your help. I understand now. I had also
completely forgotten about initializer lists, but I knew something
about my constructor didn't look right.

On Jun 2, 12:42 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
Jeff Bender <jigaboophe...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...
I am trying to remember how to code in C++ after many years of using
Java exclusively.
I have this setup:
class Base { public:
virtual void printA(){}
};
class D1 : public Base { public:
D1() {
a = 1;
}
int a;
void printA() {cout << a << endl;}
};

You should prefer 'initializer lists' (vs. assignment) in constructors:

class D1 : public Base { public:
D1() : a( 1 ){} // note the colon
int a;
void printA(){ cout << a << endl;}
};

--
Bob R
POVrookie

Jun 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
53
by: Felix Kater | last post by:
Hi, when accessing the variables in a struct: What's the reason why in C you have -> and . instead of only . ? Are there cases in which the compiler couldn't figure out what to do? Felix
5
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler...
4
by: Matthias Kaeppler | last post by:
Hi, I'm having a hard time figuring out how I can initialize a smart pointer based on a certain condition: if something then ptr = 0; // init with NULL else ptr = new XYZ; // init with a...
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.