How dumb can I get?

[ irc.freenode.org #linux-india , 16th March 2008 ]
00:10 < kstar> Let’s say the base class is called Base and the inherits are Inh1 Inh2 etc
00:11 < kstar> I want to have, elsewhere in my program, a variable declared as class Base a;
00:11 < kstar> And I want to assign the contents of class Inh1 b; to it
00:11 < kstar> Using something like a = b
00:11 < kstar> Is that possible?
00:12 < HereBeDragon> no
00:12 < jsemmanue1> HereBeDragon: why not?
00:12 < jsemmanue1> say a is of type car and b is of type maruti800
00:12 < jsemmanue1> can’t we do that?
00:13 < HereBeDragon> no, this cast from base to derived is undefined
00:13 -!- sudhanwa [n=sudhanwa@61.17.74.127] has left ##linux-india [“Kopete 0.12.5 : http://kopete.kde.org”%5D
00:13 < HereBeDragon> you can cast a maruti800 to a car. but not the other way rounf
00:13 < devdas> not a type conflict
00:13 < devdas> think shallow copy vs deep copy
00:13 < HereBeDragon> kstar: what you should do is:
00:13 < kstar> HereBeDragon: ?
00:13 < HereBeDragon> base b = new Inh1();
00:13 < devdas> oh wait, from child to parent? nope
00:13 < kstar> HereBeDragon: Eh?
00:13 < jsemmanue1> but he wants to cast from derived to parent right?
00:14 < HereBeDragon> jsemmanue1: no, he wants to cast from base to derived
00:14 < jsemmanue1> a is base and b is derived
00:14 < kstar> Rather, I’ll explain my situation more clearly
00:14 < HereBeDragon> look at it this way – when you declare a car, the size of your class is 5 bytes. When you inherit and create a
00:15 < HereBeDragon> now, you see where the casting problem comes in
00:15 < jsemmanue1> are we referring to the maruti800 or copying the entire contents?
00:15 < HereBeDragon> we are assigning
00:15 < jsemmanue1> because if we are just referring, base then doesn’t need to know that there are 3 more bytes, it just uses those that are needed
00:15 < jsemmanue1> anyway, i am firing up my netbeans to check it out
00:16 < HereBeDragon> kstar: so the solution is, give b the extra 3 bytes, so you can declare it as base b = new Inh1();
00:16 < kstar> HereBeDragon: ok
00:16 < kstar> you mean base *b
00:16 < kstar> HereBeDragon: Eh????
00:18 < kstar> HereBeDragon: Wait a minute… I’ll try to gather words to explain what exactly I want
00:18 < HereBeDragon> kstar: if you can withstand some serious flames and gain in the process, go to ##c++
00:18 < kstar> HereBeDragon: That’s the trouble. ๐Ÿ˜€
00:18 < HereBeDragon> hee hee
00:18 < HereBeDragon> c’mon, a little flame can’t hurt
00:18 < kstar> HereBeDragon: I never learnt C++ but am coding in C++
00:19 < HereBeDragon> I guessed ๐Ÿ˜€
00:19 -!- devdas [n=a@dialup-mum-203.94.233.110.mtnl.net.in] has quit [Read error: 104 (Connection reset by peer)]
00:20 < kstar> HereBeDragon: I have a class Base *a passed to my function, where a actually points to a class Inh1 *
00:20 < HereBeDragon> that’s perfectly fine
00:20 -!- TracerBullet [n=calvin@2001:470:1f06:46a:0:0:0:2] has joined ##linux-india
00:21 < TracerBullet> yay!!!
00:21 < kstar> HereBeDragon: I’m trying to look for a classdup like an strdup!
00:21 * TracerBullet kicks jsemmanuel
00:22 < kstar> HereBeDragon: But I want to make a copy of it and I don’t know which derived class it is – Inh1, Inh2 or what
00:22 < grub> TracerBullet, tunnel?
00:22 < TracerBullet> yeah
00:22 < HereBeDragon> but woe be unto you if you refer to a base as a derived without the protection of dynamic_casr
00:22 < grub> which one? i am too lazy to trace :p
00:22 < TracerBullet> now to make my website ipv6 enabled ๐Ÿ™‚
00:22 < TracerBullet> he.net
00:22 < grub> ah, he
00:23 < HereBeDragon> kstar: you can use dynamic cast for that
00:23 < grub> TracerBullet, pick up nice cool public domains from afraid.org setup ptrs, and sell vhosts for irc :p
00:23 -!- TracerBullet [n=calvin@2001:470:1f06:46a:0:0:0:2] has quit [“wooo hooo .. back on ipv6 after ages!”]
00:24 * Hobbes` goes to check afraid.org
00:26 < HereBeDragon> nice
00:26 < kstar> HereBeDragon: Thanks for being patient with me. :-S
00:26 < jsemmanue1> kstar
00:26 < jsemmanue1> is this what you want?
00:26 < HereBeDragon> kstar: no probs. But just wanted to caution you that if you’re having to use dynamic_cast() still you should reexamine
your design ๐Ÿ˜‰
00:27 < jsemmanue1> http://pastebin.com/m371b3355
00:27 < jsemmanue1> just check if this is the code you wanted? is that how you wanted the argument to be passed?
00:27 < jsemmanue1> a is the base class and b is the inherited
00:28 < jsemmanue1> tracerbullet what was that kick for?
00:28 < jsemmanue1> kstar ??
00:28 < jsemmanue1> ๐Ÿ˜€
00:28 * kstar slaps himself hard
00:28 -!- munichlinux [n=Prashant@121.243.205.67] has quit [Remote closed the connection]
00:28 < jsemmanue1> no prob. happens often with me tool
00:29 * jsemmanue1 kicks tracerbullet
00:29 -!- tuxmaniac [n=aanjhan@unaffiliated/tuxmaniac] has joined ##linux-india
00:29 < jsemmanue1> oh, he ran away
00:29 < kstar> HereBeDragon: Look at that pastebin. That’s what I wanted to achieve. ๐Ÿ˜€
00:30 < kstar> HereBeDragon: Anyway, thanks. I learnt something new/
00:30 -!- pravins [n=psatpute@59.184.30.45] has joined ##linux-india
00:32 < HereBeDragon> kstar: kewl! and read stroustroup so that you can (shakily at least) confront the masses in ##c++

And this is what the pastebin was:
//
// File: newmain.c
// Author: jsemmanuel
//
// Created on March 16, 2008, 12:10 AM
//

#include <stdlib.h>
#include <iostream.h>

//
//
//

using namespace std;
class a {
public:
int a;
};
class b: public a{
public:
int b;
int c;
};
void test(a av) {
cout<<av.a;
};
int main(int argc, char** argv) {
b b1;
b1.b=1;
b1.c=2;
b1.a=1;
test(b1);
return (EXIT_SUCCESS);
};

Advertisements