Why does this work?

This is a discussion on Why does this work? within the C++ Programming forums, part of the General Programming Boards category; The following is quite long but ignore the class declarations. Im confused about the address assignments to the object pointers ...

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    Why does this work?

    The following is quite long but ignore the class declarations. Im confused about the address assignments to the object pointers in main()

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    class twodshape {
    	double x,y;
    	char name[20];
    public:
    	virtual double area() {
    		cout<<"Must be redefined."<<endl;
    		return 0.0;
    	}
    	double X() { return x; }
    	double Y() { return y; }
    	void X(double a) { x=a; }
    	void Y(double a) { y=a; }
    	twodshape(double a,double b,char *str) {
    		x=a;
    		y=b;
    		strcpy(name,str);
    	}
    };
    class rectangle : public twodshape {
    	bool sqr;
    public:
    	double area() {
    		return X()*Y();
    	}
    	rectangle(double a,double b):twodshape(a,b,"rectangle") {
    		if(a==b) sqr=true;
    	}
    	bool Sqr() { return sqr; }
    };
    int main() {
    	twodshape *obs[3];
    	obs[0]=&rectangle(10,10); //here (isn't this the same as saying p=&5 instead of p=&var)
    	obs[1]=&rectangle(5,15); //and here
    	obs[2]=&twodshape(7,8,"generic"); //and here
    	cout<<"Shape 1 area: "<<obs[0]->area()<<endl;
    	cout<<"Shape 2 area: "<<obs[1]->area()<<endl;
    	cout<<"Shape 3 area: "<<obs[2]->area()<<endl;
    	return 0;
    }
    First time I saw it I dismissed it as wrong, but decided to try compiling it anyway. But it actually worked. What I dont understand is how a pointer can be assigned to an object that isnt even declared properly. Or is there something I missed out...
    I AM WINNER!!!1!111oneoneomne

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It didnt work!
    It just looked like it worked.

    twodshape *obs[3];
    obs[0]=&rectangle(10,10);

    The first line is fine. The second is problematical. This says make a temporary object( of lifetime until the end of the expression its used in) of class rectangle using (10,10) as constructor parameters and take the address of it and stuff it in obs[0]. Why do you think this is a problem?
    Why do you think it worked?
    Exactly same problem done twice more.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    of lifetime until the end of the expression its used in
    but then wouldn't the pointer be pointing to a non-existent object after the assignment expression? If this is true, why do subsequent expressions behave as if the pointer is pointing to a real object?

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because the data is still there even though the memory is deallocated. The program has no need to fill the data with 0's or anything as that would just be a waste of time. The point is it's only a matter of time before another object takes its place and ovewrites all or part of the data and if you try to edit it, you risk editting something else that was allocated in the same place as the temporary object was.

  5. #5
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    dang... so much pros..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21