-
November 21st, 2010, 11:10 AM
#1
A Hole in the C++ Type System
Last edited by Cesc van Fei; November 24th, 2010 at 10:04 AM.
-
November 21st, 2010, 11:22 AM
#2
Re: A Hole in the C++ Type System
Originally Posted by Cesc van Fei
Explain why, and give an example program that demonstrates the problem.
Is that a demand? Why is it bolded?
It's your homework, not ours, so let's see your answer and we will comment on it.
Regards,
Paul McKenzie
-
November 21st, 2010, 11:24 AM
#3
Re: A Hole in the C++ Type System
This sounds like homework.
-
November 21st, 2010, 01:49 PM
#4
Re: A Hole in the C++ Type System
One of the "Effective" series covers this specific problem in an Item, but I can't recall which one. Probably Effective C++. The item would be called something like "Don't use polymorphic arrays".
-
November 21st, 2010, 02:04 PM
#5
Re: A Hole in the C++ Type System
A few clues....
array[i] = *(array + i)
try sending an array of derived to a function expecting an array of base. What happens? Can you work out why?
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
November 21st, 2010, 03:05 PM
#6
Re: A Hole in the C++ Type System
Unfortunately, we can't really give you a hint as to why the behavior is bad without actually telling you. Try this
Code:
class A {
public:
A(){i = 0;}
int i;
};
class B : A {
public:
B(){j = 0xFFFFFFFF;}
unsigned int j;
};
Create an array of A and an array of class B, look carefully at the memory. Why you can't do that should become obvious.
-
November 22nd, 2010, 09:24 AM
#7
Re: A Hole in the C++ Type System
Code:
#include <iostream.h>
class B {
public:
char dataB;
B(char x) {
dataB=x;
}
void showB() {
cout << dataB;
}
};
class D: public B {
public:
char dataD;
char dataX;
D(char x, char y):B(x) {
dataD=y;
}
void show() {cout << dataB << " " << dataD <<'\n';}
};
void process(B* p) {
p->dataB='Z';
}
void process(B* a, int n) {
a[1].dataB='H';
}
int main() {
int n=2;
D ob[]={D('B','D'),D('P','Q')};
//process(ob+1);
process(ob,n);
for (int i=0;i<n;i++) {
ob[i].show();
}
return 0;
}
Sorry, I forgot posting my solution. Any comments for me?
Does someone have some hints for me?
Thanks very much!
-
November 22nd, 2010, 11:17 AM
#8
Re: A Hole in the C++ Type System
iostream.h is very very deprecated. The correct header is <iostream>
We can see whats happening in your code, but your mission is to explain why and we do not see any explanation for the exhibited behaviour.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
November 22nd, 2010, 12:13 PM
#9
Re: A Hole in the C++ Type System
I wrote some code that shows you the behavior you are talking about, do you see why the output is the way that it is?
Code:
#include <iostream>
class A {
public:
A(){i = 0;}
unsigned int i;
};
class B : public A {
public:
B(){j = 0xFFFFFFFF;}
unsigned int j;
};
static void printA(A * array, size_t len){
for(size_t k = 0; k < len; ++k, ++array){
std::cout << (*array).i << std::endl;
}
}
static void printB(B * array, size_t len){
for(size_t k = 0; k < len; ++k, ++array){
std::cout << (*array).i << std::endl;
}
}
int main(void){
B testArray[10];
printA((A*)testArray, 10);
printB(testArray, 10);
return 0;
}
-
November 22nd, 2010, 07:35 PM
#10
Re: A Hole in the C++ Type System
Originally Posted by Cesc van Fei
Code:
#include <iostream.h>
class B {
public:
char dataB;
B(char x) {
dataB=x;
}
void showB() {
cout << dataB;
}
};
class D: public B {
public:
char dataD;
char dataX;
D(char x, char y):B(x) {
dataD=y;
}
void show() {cout << dataB << " " << dataD <<'\n';}
};
void process(B* p) {
p->dataB='Z';
}
void process(B* a, int n) {
a[1].dataB='H';
}
int main() {
int n=2;
D ob[]={D('B','D'),D('P','Q')};
//process(ob+1);
process(ob,n);
for (int i=0;i<n;i++) {
ob[i].show();
}
return 0;
}
Sorry, I forgot posting my solution. Any comments for me?
Does someone have some hints for me?
Thanks very much!
1) What is an array? Given that, how does the compiler know where each array element starts? For example, if you have an array of Base, and Base[0] is the first element in memory, how does the compiler know where Base[1] is in memory, Base[2], Base[3], etc.? Please explain this.
2) Given your answer for 1) above, if the compiler expects an array of Base, but the array has actually an array of Derived objects (where Derived is derived from Base), and that Derived objects are larger in size than Base objects (a huge clue), what do you think the compiler will do, given your answer above?
That, in a nutshell, is your answer.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 22nd, 2010 at 07:37 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|