Jump to content
  • Advertisement
Sign in to follow this  
frogtag

NEW PROBLEM - Inheritance Class Deconstructor Problem

This topic is 3706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone tell me why when I compile, I get the message that consolefont is undeclared in my deconstructor? Is it because I can't call Font consolefont within the Console constructor as it makes it a local variable? console.cpp
#include "console.h"

Console console;

Console::Console()
{
  Font consolefont("Arial",12);
  consolefont.BuildFont();
}

Console::~Console()
{
  consolefont.DeleteFont();
}



console.h
#ifndef CONSOLE_H
#define CONSOLE_H

#include "font.h"

class Console : public Font
{
  private:

  public:
    Console();
    ~Console();
};

extern Console console;

#endif



font.h
#ifndef FONT_H
#define FONT_H

#include <windows.h>
#include <gl\gl.h>

#include "window.h"

class Font
{
  private:

	public:
		Font();
		Font(char*,int);

		void BuildFont(void);
		void DeleteFont(void);
		void Print(char*, ...);
		
		int	base;

		char* fontface;
		int height;
};

extern Font font;

#endif



[Edited by - frogtag on May 24, 2008 2:06:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
You answered your own question. consolefont is a local variable in the Console constructor and goes out of scope when the constructor returns.

If you need to use the consolefont throughout the existence of the Console object, make it a member of the Console class.

Share this post


Link to post
Share on other sites
How do I make consolefont a member of the Console class if it's already part of the Font class?

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
How do I make consolefont a member of the Console class if it's already part of the Font class?


You could still have a Font member of Console, even if Console is derived from Font.

To be honest, though, it seems very strange to derive Console from Font. Unless Console is a specialised form of Font (which I'm assuming it isn't from the class name), it would make more sense for Console to just contain a Font object, or perhaps a list<Font> to store multiple fonts in if required.

Why are you deriving Console from Font?

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
Is it because I can't call Font consolefont within the Console constructor as it makes it a local variable?


Yes.

This should fix things:


#include "console.h"

/* This forward declaration is unnecessary*/
//Console console;

Console::Console() : Font("Arial",12) /* Call the superclass constructor here*/
{
BuildFont();
}

Console::~Console()
{
DeleteFont();
}



However as noted before, your design choices in your class hierarchy are somewhat suspect.

Share this post


Link to post
Share on other sites
Simply put ...
the font class is a bitmap font class. the console class will be a game console. all i'm trying to do is initialise a font for the console only, independant of any other part of the program. The console class will initialise its own font, use that font, then deconstruct the font when it deconstructs itself.

(I like neat and tidy programming!)

Share this post


Link to post
Share on other sites
consolefont isn't a member of the Font class. It's an instance of the Font class.

To add consolefont as a member variable to Console:

class Console
{
private:
Font consolefont; // consolefont is a private member of Console

public:
Console();
~Console();
};




This way consolefont will be accessible to all the methods of the Console class. If it needs parameters for its construction, you can provide them in the Console constructor's initializer list:


Console::Console()
: consolefont("Arial",12)
{
}




If you aren't familiar with the concepts I used here, I suggest you read up on classes and how to use them.

EDIT: up until others pointed out I missed that Console is derived from Font. That makes no sense, don't do that. Inheritance is an is-a relationship, meaning the derived class is-a superclass. A Console most definitely is-not-a Font.

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
Simply put ...
the font class is a bitmap font class. the console class will be a game console. all i'm trying to do is initialise a font for the console only, independant of any other part of the program. The console class will initialise its own font, use that font, then deconstruct the font when it deconstructs itself.

(I like neat and tidy programming!)


So you want a Font member of the Console class, not to derive Console from Font.

Rather than:


class Console : public Font
{
// in simple terms, this is saying that a Console IS A specialised type of Font
};


You want


class Console
{
private:
Font font;

public:
Console() : font("Arial",12) { font.BuildFont(); }
~Console(){ font.DeleteFont(); }
};


Perhaps BuildFont() and DeleteFont() could be moved into the Font's constructor and destructor as well?

You certainly should be okay to call them Build() and Delete() as it should be pretty obvious from the object you are calling them on that you are building and destroying a font.

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
Simply put ...
the font class is a bitmap font class. the console class will be a game console. all i'm trying to do is initialise a font for the console only, independant of any other part of the program. The console class will initialise its own font, use that font, then deconstruct the font when it deconstructs itself.

(I like neat and tidy programming!)


In inheritance implies a "is-a" relationship. When you inherited console from font you're saying that a console "is-a" font, which it isn't. A console uses fonts. That tells you you should have an instance of the Font class within your console class.

Perhaps a better hierarchical relationship you could have established is: "BoldFont" derives from Font. Thats because a "BoldFont" "is-a" type of "Font".

Share this post


Link to post
Share on other sites
Get where you're coming from and the confusion i'm causing. No console isn't a specialised font. Just though by using inheritance I could use the members of font within console. I think with the info given by everyone (thank you all) I should be able to sort it out.

Sorry for confusing you all!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!