Jump to content
  • Advertisement
Sign in to follow this  

Using classes in other classes

This topic is 4719 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

For example, i have three classes. 1. Main 2. MainSub 3. Log MainSub and Log are both declared in Main. How can i use the log class in the mainsub class ? So you will get:
void MainSub::Something()
{
log.write("error");
}


The way i do it right now is giving the log pointer trough to the MainSub class (void MainSub::Something(Log *log)). I'm wondering if there are other ways of doing this.

Share this post


Link to post
Share on other sites
Advertisement
Hello twan,

If log is global or is file scope you can just have a log class use.

global:
extern class Log;
define some were else or in this file just don't put extern.

file scope:

static class log; only seen in this file and init before running.

Does it need to have log as a data member:

class MainSub
{
...
class Log log;
...
};

If you want MainSub to have an instance that reference a class log.
Then you can use a pointer to log class or a refernece.

class MainSub
{
MainSub(Log* l) : log(l)
...
class Log* log;
...
};

or

class MainSub
{
MainSub(Log& l) : log(l)
...
class Log& log;
...
};

Is log meant to be globaly used?

Lord Bart :)

Share this post


Link to post
Share on other sites
No i don't want to declare Log global.

When loading i set the file where the log class has to write in. When i use the log class in the MainSub it need to write to the same file.

I used your sample code to create the following sample code but i get a syntax error expected member function.


class Log
{
public:
void Print();


};

class MainSub
{
MainSub(Log &l) : log(l);

class Log &log;



};

class Main
{
Log log;
MainSub MainSub(log);
};

Share this post


Link to post
Share on other sites
Don't need the class in front of Log in MainSub.

class MainSub
{
MainSub(Log &l) : log(l) {} // I made a typo error here ; should have been {}
// sorry
Log &log;
};

Also you can't initialize a data member at declaration.
Try this:
class Main
{
Main() : MainSub(this->log) {}
Log log;
MainSub MainSub;
};

The problem is you want at initialize MainSub with log that comes form Main.
Since Log is decleared first in Main it will be initialize first.
Then you should be able to pass log to MainSub.

Lord Bart :)

[Edited by - Lord Bart on June 16, 2005 2:43:39 PM]

Share this post


Link to post
Share on other sites
I tried what you've written but i still get this error:
error C2969: syntax error : ';' : expected member function definition to end with '}'.

The error is pointing to this line:
MainSub(Log &l) : log(l);

Share this post


Link to post
Share on other sites
Quote:
Original post by twan
I tried what you've written but i still get this error:
error C2969: syntax error : ';' : expected member function definition to end with '}'.

The error is pointing to this line:
MainSub(Log &l) : log(l);


If you have the implementation for this constructor later, then it might work with
MainSub(Log &l);

and in the implementation

MainSub(Log &l) : log(l) {
// code
}

if not then in the class you could put
MainSub(Log &l) : log(l) { }

Share this post


Link to post
Share on other sites
Quote:
Original post by twan
I tried what you've written but i still get this error:
error C2969: syntax error : ';' : expected member function definition to end with '}'.

The error is pointing to this line:
MainSub(Log &l) : log(l);


A constructor initializer list is not part of the function declaration, but rather a supplement. To fix the error, give the constructor an empty body, i.e:

MainSub(Log &l) : log(l) {}

Share this post


Link to post
Share on other sites
One way to do it is to make the log class a singleton, but my understanding is that singletons are a basically a better way to use globals when you have to use globals.


Otherwise a pointer to the log should work

Here's one possible solution (maybe not the best way - it is a bit pointer happy)


class Main
{
Log* mp_log;
MainSub* mp_MainSub;

void init();
}

Main::init()
{
mp_Log = new Log;
mp_MainSub = new MainSub(mpLog);
}


class MainSub
{
Log* m_pointerToLog;

public:
MainSub(Log* lo);
Something();
}

MainSub::MainSub(Log* lo)
{
m_pointerToLog = lo;
}

void MainSub::Something()
{
m_pointerToLog->write("error");
}







This is just off the top of my head (the basic idea should work when you implement the log class), someone please correct me if there's a better way to do this.

Also make sure when you delete pointers you set them to 0, and always check that a pointer is not equal to 0 before you use it (i'll leave that up to you to add that).

[Edited by - Will F on June 17, 2005 2:21:06 PM]

Share this post


Link to post
Share on other sites
If Log and MainSub are only going to be for your Main class, you can define them inside your main class. Example:
class Main {
Main();
void Something();
typedef class MainSub {
void Muffin() { Bleh; }
void Bagel();
int cheese;
Main* owner; //I own you!
} main_sub;
typedef class Log {
void Write();
int author;
Main* owner;
} log;
};
//Define Main's function[s]:
Main::Main() {
log.owner=this;
main_sub.owner=this;
}
void Main::Something() {
main_sub.Muffin(); //Execute main_sub's Muffin()
log.Write(); //Execute log's Write()
}
//Define MainSub's function[s]:
void Main::MainSub::Muffin() {
//Communicate with my owner's log
owner->log.Write();
}
//Define Log's function[s]:
void Main::Log::Write() {
//Written by the author
}






Something along those lines.
EDIT: Well, crap... Didn't completely read your post, just saw all of the other posts and thought you wanted something like this. What you can do is make pointers in the Log class and MainSub class pointing to the class they're inside of. I edited it some.
Was that what you wanted?

NOTE: If you want to get super complicated, which you probably don't, and you don't want to store a pointer and initialize a pointer in each class, you can calculate offsets (just make sure that both of them are first).
void Main::MainSub::Muffin() {
//Get the Log
Log *log=(Log*)this+sizeof(Main::MainSub);
log->Write();
}
void Main::Log::Write() {
//Get the MainSub
MainSub *main_sub=(MainSub*)this-sizeof(Main::MainSub);
main_sub->Bagel();
}

Share this post


Link to post
Share on other sites
Thanks Will F, (and also the rest) it works like i wanted and i also understand what you've typed :).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!