C++ 如何使用c++;在其他文件中定义的对象?
我一直有这个问题。如果我在main.cc中定义了一个对象,我如何从另一个.cc文件访问该对象 main.cc:C++ 如何使用c++;在其他文件中定义的对象?,c++,C++,我一直有这个问题。如果我在main.cc中定义了一个对象,我如何从另一个.cc文件访问该对象 main.cc: #include "Class.h" #include "header.h" int main() { Class object; return 0; } #include "Class.h" #include "header.h" int main() { Class* object = new Class; function(*obje
#include "Class.h"
#include "header.h"
int main()
{
Class object;
return 0;
}
#include "Class.h"
#include "header.h"
int main()
{
Class* object = new Class;
function(*object); // <--- pass the object by reference
return 0;
}
#include "Class.h"
#include "header.h"
int main()
{
Class* object = new Class;
//
// I recommend you do not pass instance to function.
//
// Instead, the C++ way is to invoke an instance method:
object->function();
// and, if you've been paying attention, you know that the method
// Class::function()
// has access to the 'this' pointer of the class
// and thus the 'passing' of this instance information
// is already coded!
// some of your peers would say you must:
delete object;
// others would say this is already accomplished by the task exit.
return 0;
}
file.cc:
#include "header.h"
void function()
{
object.method(parameter);
}
#include main.cc
#include myclasses.h
#include "Class.h"
#include "header.h"
void function(Class& object) // <-- how to specify reference
{
object.method(parameter); // <-- you did not identify parameter
}
我需要在header.h中添加什么才能使其正常工作?任何帮助都将不胜感激。如果您想尝试像 file.cc:
#include "header.h"
void function()
{
object.method(parameter);
}
#include main.cc
#include myclasses.h
#include "Class.h"
#include "header.h"
void function(Class& object) // <-- how to specify reference
{
object.method(parameter); // <-- you did not identify parameter
}
编译器将编译main.cc两次——这意味着它将在main.cc中看到所有内容的两个定义。这会给你带来一些问题
为类创建一个自定义头文件,然后根据需要导入,这是更好的设计(而且实际上编译正确!额外!)
myclasses.h:
Class object;
file.cc:
#include "header.h"
void function()
{
object.method(parameter);
}
#include main.cc
#include myclasses.h
#include "Class.h"
#include "header.h"
void function(Class& object) // <-- how to specify reference
{
object.method(parameter); // <-- you did not identify parameter
}
如果你想尝试像 file.cc:
#include "header.h"
void function()
{
object.method(parameter);
}
#include main.cc
#include myclasses.h
#include "Class.h"
#include "header.h"
void function(Class& object) // <-- how to specify reference
{
object.method(parameter); // <-- you did not identify parameter
}
编译器将编译main.cc两次——这意味着它将在main.cc中看到所有内容的两个定义。这会给你带来一些问题
为类创建一个自定义头文件,然后根据需要导入,这是更好的设计(而且实际上编译正确!额外!)
myclasses.h:
Class object;
file.cc:
#include "header.h"
void function()
{
object.method(parameter);
}
#include main.cc
#include myclasses.h
#include "Class.h"
#include "header.h"
void function(Class& object) // <-- how to specify reference
{
object.method(parameter); // <-- you did not identify parameter
}
如何从另一个.cc文件访问该对象?
我需要在header.h中添加什么才能使其正常工作
简单的答案是“通过引用传递对象”
在main()中创建的对象将持续整个程序。这在嵌入式系统中是典型的。。。我在这方面没有问题
但是,我会将持久对象放在动态内存中(因为堆栈更有限)
main.cc:
#include "Class.h"
#include "header.h"
int main()
{
Class object;
return 0;
}
#include "Class.h"
#include "header.h"
int main()
{
Class* object = new Class;
function(*object); // <--- pass the object by reference
return 0;
}
#include "Class.h"
#include "header.h"
int main()
{
Class* object = new Class;
//
// I recommend you do not pass instance to function.
//
// Instead, the C++ way is to invoke an instance method:
object->function();
// and, if you've been paying attention, you know that the method
// Class::function()
// has access to the 'this' pointer of the class
// and thus the 'passing' of this instance information
// is already coded!
// some of your peers would say you must:
delete object;
// others would say this is already accomplished by the task exit.
return 0;
}
如何从另一个.cc文件访问该对象?
我需要在header.h中添加什么才能使其正常工作
简单的答案是“通过引用传递对象”
在main()中创建的对象将持续整个程序。这在嵌入式系统中是典型的。。。我在这方面没有问题
但是,我会将持久对象放在动态内存中(因为堆栈更有限)
main.cc:
#include "Class.h"
#include "header.h"
int main()
{
Class object;
return 0;
}
#include "Class.h"
#include "header.h"
int main()
{
Class* object = new Class;
function(*object); // <--- pass the object by reference
return 0;
}
#include "Class.h"
#include "header.h"
int main()
{
Class* object = new Class;
//
// I recommend you do not pass instance to function.
//
// Instead, the C++ way is to invoke an instance method:
object->function();
// and, if you've been paying attention, you know that the method
// Class::function()
// has access to the 'this' pointer of the class
// and thus the 'passing' of this instance information
// is already coded!
// some of your peers would say you must:
delete object;
// others would say this is already accomplished by the task exit.
return 0;
}
能否在另一个文件(myclasses.h)中定义该类,然后在两个.cc文件中导入该头文件?在前几章中,它会教你这方面的知识。你能在另一个文件(myclasses.h)中定义这个类,然后在两个.cc文件中导入这个头文件吗?它将在前几章教你这方面的知识。