Sign in to follow this  

NEW PROBLEM - Inheritance Class Deconstructor Problem

This topic is 3491 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
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
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
Okay, i've implemented the code that everyone has helped me with. It compiled fine, however, the test text displays on the screen but is garbage. I've played around and found that the Console constructor isn't calling the BuildFont member? Can anyone tell me why it won't?

console.cpp

#include "console.h"
Console console;
Console::Console() : consolefont("Arial",24)
{
consolefont.BuildFont();
}
Console::~Console()
{
consolefont.DeleteFont();
}
void Console::Render()
{
glRasterPos2f(-0.40f, 0.35f);
consolefont.Print("Test String 1");
};





console.h

#ifndef CONSOLE_H
#define CONSOLE_H

#include "font.h"

class Console
{
private:
Font consolefont;
public:
Console();
~Console();

void Render(void);
};

extern Console console;

#endif




C++/OpenGL/VC++ 2008

Share this post


Link to post
Share on other sites
Is the Console c'tor even called? If not then this might be a problem with the order of initialization for global variables. Try this:


Console & getConsole() {
static Console c;
return c;
}

// Instead of console, use getConsole()
getConsole().Render();



You might have to do the same with the global Font object. See here for more info (I'm using the second technique suggested there).

Share this post


Link to post
Share on other sites
This is where I come across as even more stupid...

The console constructor is called in the first line of the console(.cpp) source file.

Also

I followed the link you gave and it's for Linux. I'm using c++ / OpenGL API and Visual C++ 2008.

[EDIT]

Okay, i think i might understand where you're coming from (bare with me) ...

When the console is called, first it initialises its constructor, then sets up the font (Font consolefont) afterwards ... is this the wrong way round?

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
I followed the link you gave and it's for Linux.


No, it's general C++ and is applicable to any platform.

Quote:
The console constructor is called in the first line of the console(.cpp) source file.


Are you sure? Did you step through the program with a debugger and saw that it is entering the constructor?

Share this post


Link to post
Share on other sites

This topic is 3491 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this