Anyone care to help out a newbie?

This is a discussion on Anyone care to help out a newbie? within the C++ Programming forums, part of the General Programming Boards category; object orientated, classes, copy constructors, polymorphism, etc im going to type all this out and explain what i know, plz ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Anyone care to help out a newbie?

    object orientated, classes, copy constructors, polymorphism, etc

    im going to type all this out and explain what i know, plz chime in to correct me or to explain in a different way or in more detail as im still not sure about it all.

    Code:
        class CLocation
        {
        	public:
        		double x;
        		double y;
        };



    Code:
        class CInvader
        {
        	// Other code
        	CInvader(CLocation position);
        	// Other code
        };


    Code:
        CInvader::CInvader(CLocation position)
        {
        	// Other code
        	this->x = position.x;
        	this->y = position.y;
        };

    then later we can write
    Code:
    CPosition pos;
    pos.x=200;
    pos.y=400;
    CInvader anInvader(pos);

    problem with the constructor is it can get get called when you imply that you want it

    e.g

    Code:
    void Attack(CInvader inv)
    {
    // Blah blah
    }
    
    // Somewhere in rest of program
    CPosition invader1pos;
    invader1pos.x = 200;
    invader1pos.y = 300;
    CInvader invader1(invader1pos);
    // and later…
    Attack(invader1pos); // Typo
    I am trying to attack an invader using the Attack(CInvader inv) function, but I have mis-typed.
    invader1pos belongs to the CPosition class, but it can still be sent to the function, the compiler simply casts the paramater type to the class type.

    in this case a CPosition was sent in error instead of a CInvader but was able to create 1 by casting. problem is the new CInvader runs out of scope sooner than a copy of the object would (not sure i get that bit, is it local within the attack function?)

    Edit: I apologise for not fully using code tags first time...
    Last edited by televisual; 10-02-2008 at 01:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    To prevent this implicit casting, use the keyword explicit, e.g.,
    Code:
        class CInvader
        {
        	// Other code
        	explicit CInvader(CLocation position);
        	// Other code
        };
    By the way, you should be passing objects by (const) reference instead of by value, unless you really want to make a copy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Smile

    Thank you lazerlight...

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    By the way, you are half-way there:

    Code:
        CInvader::CInvader(CLocation position)
        {
        	// Other code
        	this->x = position.x;
        	this->y = position.y;
        };
    There probably isn't much reason why CInvader should have two double members, if it could have just one CLocation member, resulting in this (and quite possibly shortening code elsewhere too):

    Code:
        CInvader::CInvader(CLocation position)
        {
        	// Other code
        	this->location = position; //this-> is optional
        };
    Another thing is that you could give a constructor to CLocation that takes two doubles (and possibly also a default constructor). Then you could construct the CInvader like this:

    Code:
    // Somewhere in rest of program
    //CPosition invader1pos;
    //invader1pos.x = 200;
    //invader1pos.y = 300;
    
    CInvader invader1(CPosition(200.0, 300.0));
    Since you can now use an unnamed temporary there would be less "useless" objects around that you can accidentally misuse - although naturally the explicit keyword helps too.
    Last edited by anon; 10-02-2008 at 01:32 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Thanks Anon... some nice tips there.


  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    regarding deep copy of objects, i understand its necassary to say you need a pointer to point to another memory address and not what the other is pointing to.

    assuming a CSprite class had a pointer to a MyPicture class as 1 of the private variables (and that copy is a function of MyPicture.

    Code:
        CSprite::CSprite(const CSprite& otherCSprite)
        {
        	height = otherCSprite.height;
        	width = otherCSprite.width;
        	mpTheImage = new MyPicture;
        	mpTheImage->copy(&otherCSprite.mpTheImage);
        }

    im told i would need to overload the assignment operator (=) in order to perform this deep copy but surely as long as i got the above, simply doing

    CSprite FirstSprite;
    CSprite SecondSprite = FirstSprite;

    it would go through that copy constructor no problem.

    i have another question regarding overloading the operator but ill do that in the next post as its a new question.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by televisual View Post
    regarding deep copy of objects, i understand its necassary to say you need a pointer to point to another memory address and not what the other is pointing to.

    assuming a CSprite class had a pointer to a MyPicture class as 1 of the private variables (and that copy is a function of MyPicture.

    Code:
        CSprite::CSprite(const CSprite& otherCSprite)
        {
        	height = otherCSprite.height;
        	width = otherCSprite.width;
        	mpTheImage = new MyPicture;
        	mpTheImage->copy(&otherCSprite.mpTheImage);
        }
    im told i would need to overload the assignment operator (=) in order to perform this deep copy but surely as long as i got the above, simply doing

    CSprite FirstSprite;
    CSprite SecondSprite = FirstSprite;

    it would go through that copy constructor no problem.

    i have another question regarding overloading the operator but ill do that in the next post as its a new question.
    That would do the copy constructor, yes. But
    Code:
    CSprite SecondSprite;
    SecondSprite = FirstSprite;
    would use operator=, not the constructor.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Thanks very much.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  5. the us constitution
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 121
    Last Post: 05-28-2002, 04:22 AM

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