Thread: Question about classes

  1. #1
    Registered User
    Join Date
    Jun 2010

    Question about classes

    I'm trying to write a clone of Galaga using the allegro library and have been using classes. I have a class called enemy and I'm wondering why this isn't a valid call to the destructor
    class Enemy {
    	int health;
    	int x_position;
    	int y_position;
    	bool collision;
    	BITMAP *image;
    	Enemy(int x, int y,int h = 1){
    	x_position = x;
    	y_position = y;
    	health = h;
    	bool collision=false;
    	image = load_bitmap("enemyShip.bmp", NULL);
    	//class functions
    	int get_health(){return health;}
    	void movement(int&x,int&y);
    then I have the function definiton for movement here

    void Enemy::movement(int &x, int &y)
    		//try to add support for sin movement across screen
    	if (collision==true){~Enemy();}

    is it not valid to call the destructor inside a class function?

    this is the compiling error

    error C2512: 'Enemy::Enemy' : no appropriate default constructor available

    I'm figuring this means that it's thinking I'm trying to call a constructor? If so, am I mistaken that the ~Enemy() call isn't a destructor, I was under the impression that it was.

    Sorry if this is a stupid question.
    Thanks for the Help,
    Last edited by mjhamrick; 06-26-2010 at 07:47 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    I'm going to respond to your question with another question: does your code use placement new?

    If you don't know what placement new is, or if your code does not use it, then you should NEVER explicitly call a destructor.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2010
    I can't say I know what placement new is, but my idea was that when the enemy got shot it would destruct the object? Now that I say that I guess I don't know if the destructor destructs the class, or an instance of an object. I might be trying something pointless here, I'm not sure. I just thought that might work for what I'm trying to do. The idea is that if collision true, then I could call the destructor and it would get rid of the instance of enemy and since I have in the destructor the code to destroy the bitmap, I figured that would get it off the screen. I haven't been coding in C++ that much, about a week, and I don't have much prior experience with Object oriented programming, so I might just be thinking about this the right way. Thanks for the response.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Your object does not know how to delete itself without making wild assumptions.
    The object could exist on the stack or the heap, or could be part of an array etc. Those assumptions will get you into trouble.

    If you want to have the object be deletable then you should enforce some conditions surrounding its creation. You can do this by making all constructors private and then providing a static creation method which allocates using new, and then your function can in theory call delete *this; This is still only safe if nothing attempts to access the object after it is deleted and so is usually only done in the context of an intrusive smart pointer, which is capable of NULLing itself.

    Having said all that, why not just use a pre-made smart pointer which already safely does all of the above?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2010
    What exactly is a smart pointer?

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    Google shared_ptr. That is a smart pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 01:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 07:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

Tags for this Thread