Linux c++ .hpp 和 .cpp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ .hpp and .cpp
提问by linuxx
Can someone tell me what is the correct way to create a .hpp and .cpp that includes classes, subclasses and methods? Do I have to use export "C" firstClass* create_object { return new firstClass; }
? (I am working in C++.) Should I have file.hpp
or file.h
?
有人能告诉我创建包含类、子类和方法的 .hpp 和 .cpp 的正确方法是什么吗?我必须使用export "C" firstClass* create_object { return new firstClass; }
吗?(我在 C++ 中工作。)我应该有file.hpp
还是file.h
?
#include <string>
//public ?? how can i have this?
class firstClass
{
public:
firstClass();
class secondClass
{
public:
secondClass();
std::string name;
virtual std::string method1();
} *sec;
virtual void DoSomething();
} *first;
// And for a private class?
class private *priv;
in file.cpp
在文件.cpp
#include file.hpp
firstClass::firstClass()
{
sec = new firstClass::secondClass();
}
std::string firstClass::secondClass::method1()
{
//code
}
And now if I have to extern
an object for each class/subclass? It is necessary if I want to create an .so file and use dlsym and dlopen to access classes, subclasses and methods, modify values, send a reference to a specific method? Thanks!
现在,如果我必须extern
为每个类/子类创建一个对象?如果我想创建一个 .so 文件并使用 dlsym 和 dlopen 来访问类、子类和方法、修改值、发送对特定方法的引用,这是必要的吗?谢谢!
extern "C" firstClass* create_object()
{return new firstClass}
extern "C" secondClass* create_object()
{return new secondClass}
回答by Lstor
Whether you have file.hpp
or file.h
is just a matter of preference. Personally, I use .hpp
to indicate C++ rather than C.
你是否拥有file.hpp
或file.h
只是一个偏好问题。就个人而言,我习惯于.hpp
表示 C++ 而不是 C。
In C++, unlike for example Java, classes in the global namespace are not public
or private
. You can, however, hide or limit access to classes in various ways. For example:
在 C++ 中,与 Java 不同,全局命名空间中的类不是public
或private
。但是,您可以通过各种方式隐藏或限制对类的访问。例如:
- Having them
private
in another class. - Using namespaces.
- 让他们
private
在另一个班级。 - 使用命名空间。
A few other points:
其他几点:
- The syntax for including
file.hpp
is#include "file.hpp"
- Your
firstClass
andsecondClass
classes have virtual methods and a default destructor. You should make your destructorpublic
andvirtual
, orprotected
and non-virtual
- Classes are usually defined like this:
- 包含的语法
file.hpp
是#include "file.hpp"
- 您的
firstClass
和secondClass
类具有虚拟方法和默认析构函数。你应该让你的析构函数public
andvirtual
, orprotected
和 non-virtual
- 类通常是这样定义的:
Class definition:
类定义:
class SomeClass {
// code
};
and when you want a pointer to it, you write SomeClass* myPointer;
, rather than the way you declare first
and sec
.
当你想要一个指向它的指针时,你写SomeClass* myPointer;
,而不是你声明first
和的方式sec
。
You usually want to put class declarations and/or definitions in header files, along with function declarations. Implementations goes in the source files. Example:
您通常希望将类声明和/或定义与函数声明一起放在头文件中。实现在源文件中。例子:
// Header file:
class FirstClass {
public:
FirstClass();
void DoSomething();
};
// Source file:
FirstClass::FirstClass()
{
// code
}
void FirstClass::DoSomething()
{
// code
}
I'd recommend you to pick up a C++ textbook, where such basics are plentifully covered. My personal recommendation is Accelerated C++.
我建议您选择一本 C++ 教科书,其中涵盖了大量此类基础知识。我个人的建议是Accelerated C++。