具有多个类的 C++ 新对象

标签 c++ class object namespaces

所以我有一个主文件和两个类:一个显示类和一个作业类。

我想要实现的是让主类能够调用显示类,以及与作业类的接口(interface),但我还希望作业类能够调用显示类的方法和也将参数发送到显示类。

我已经尝试了多种方法来解决我当前的问题,但我无法完成我想要的,我听说过命名空间,但我不熟悉它们,不确定这是否是我需要的。

我也尝试过从 main 传递 Job/Display 对象,但这对我想做的事情不起作用,因为在我的标题中我已经定义了一个新对象。

这是我想要实现的一些示例代码(请忽略简单的编译器错误/这只是示例代码,我不会发布我的整个项目,因为那样会很长/忽略 header ):

main.cpp

 int main(){
    Display display;
    Job job;
    job.init();
    display.test();
    return 0;
}

显示.cpp

 void Display::test(){
     std::cout << "testing.." << std::endl;
 }

 void Display::test2(std::string ttt){
     Job job; //Do not want to create a whole new object here
     std::cout << "testing3333...." << job.getThree() << std::endl;
     std::cout << "testing2222...." <<  ttt << std::endl;
 }

作业.cpp

 void Job::init(){
      Display disp2; //I do not want to create a whole new object here, but I can't fix this
      disp2.test2("from Job");
 }

 std::string Job::getThree(){
       return "test3";
 }

工作.h

class Job{
private:
    Display disp; // Do not want a new object here as well
public:
    void init();
    std::string getThree();
};

最佳答案

您需要将一个Job 指针传递给Display,反之亦然,以便它们相互了解,例如:

主要内容:

#include "Display.h"
#include "Job.h"

int main()
{ 
    Display display; 
    Job job; 
    display.init(&job);
    job.init(&display); 
    display.test(); 
    return 0; 
} 

显示.h:

class Job;

class Display
{
private:
    Job *_job;
public:
    Display();
    void init(Job *job);
    void test();
    void test2(const std::string &ttt);
};

显示.cpp:

#include "Display.h"

Display::Display()
    : _job(NULL)
{
}

void Display::init(Job *job)
{
    _job = job;
}

void Display::test()
{ 
    std::cout << "testing.." << std::endl; 
} 

void Display::test2(const std::string &ttt)
{ 
    std::cout << "testing3333...." << _job->getThree() << std::endl; 
    std::cout << "testing2222...." << ttt << std::endl; 
} 

工作.h:

class Display;

class Job
{
private:
    Display *_display;
public:
    Job();
    void init(Display *display);
    std::string getThree();
};

作业.cpp:

#include "Job.h"

Job::Job()
    : _display(NULL)
{
}

void Job::init(Display *display)
{ 
    _display = display;
    _display->test2("from Job"); 
} 

std::string Job::getThree()
{ 
    return "test3"; 
} 

鉴于您提到的要求,Display 并不需要真正记住Job,只有Job 需要记住显示,所以你可以做这样的事情作为替代:

主要内容:

#include "Display.h"
#include "Job.h"

int main()
{ 
    Display display; 
    Job job;
    job.init(&display); 
    display.test(); 
    return 0; 
} 

显示.h:

class Job;

class Display
{
public:
    void test();
    void test2(Job *job);
};

显示.cpp:

#include "Display.h"

void Display::test()
{ 
    std::cout << "testing.." << std::endl; 
} 

void Display::test2(Job *job)
{ 
    std::cout << "testing3333...." << job->getThree() << std::endl; 
} 

工作.h:

class Display;

class Job
{
private:
    Display *_display;
public:
    Job();
    void init(Display *display);
    std::string getThree();
};

作业.cpp:

#include "Job.h"

Job::Job()
    : _display(NULL)
{
}

void Job::init(Display *display)
{ 
    _display = display;
    _display->test2(this); 
} 

std::string Job::getThree()
{ 
    return "test3"; 
} 

关于具有多个类的 C++ 新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593822/

相关文章:

javascript - 如何使用 Javascript/Jquery 显示图像对象

c++ - Boost::Lexical_cast 转换为 float 改变数据

c++ - 如何在 C++ 中使用 setiosflags 将十进制数打印为八进制数

c++ - 神级的替代品

c++ - 父子破坏顺序

c++ - 使用另一个类的对象创建一个类

python - 使用read_sas后如何从pandas对象类型的b'Text'中获取文本?

java - 父类(super class)对子类对象的引用显示与子类对子类对象的引用相同的行为

c++ - gSOAP 在请求完成后删除对象中的引用值

python - 创建一个行为类似于任何变量但在更改/读取时具有回调的类