destructor

How to correctly define and link a C++ class destructor to a main file?

牧云@^-^@ 提交于 2021-02-11 07:20:24
问题 This is a particularized question from mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status There is a user-defined class inside MyClass.hpp: class MyClass { public: MyClass(const string& className); ~MyClass() {cout << "Destructor definition instead of g++ default one?";} ; ... and you try to construct an object out of it in the main file: #include "MyClass.hpp" //in the same directory ... int main() { ... MyClass myClassObj = MyClass

How to correctly define and link a C++ class destructor to a main file?

戏子无情 提交于 2021-02-11 07:20:06
问题 This is a particularized question from mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status There is a user-defined class inside MyClass.hpp: class MyClass { public: MyClass(const string& className); ~MyClass() {cout << "Destructor definition instead of g++ default one?";} ; ... and you try to construct an object out of it in the main file: #include "MyClass.hpp" //in the same directory ... int main() { ... MyClass myClassObj = MyClass

Restricting Access to C++ Constructor and Destructor

折月煮酒 提交于 2021-02-08 13:46:24
问题 Forgive me if this has already been asked, I didn't find any answers to my specific question. I have a class in a library I'm making that I want certain classes to be able to create and destroy, and other classes to be able to access other public functions. Having a friend class is not what I want either as the friend class will get access to member variables and member functions which I don't want. I stumbled upon this idiom which almost works, except for the destructor since it can't take

Destructor call for scalar types & undefined behavior [duplicate]

故事扮演 提交于 2021-02-08 02:58:34
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

Destructor call for scalar types & undefined behavior [duplicate]

走远了吗. 提交于 2021-02-08 02:58:09
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

order of calling constructor in inheritance

你离开我真会死。 提交于 2021-02-05 08:46:34
问题 I am new to C++ programming language, i have a confusion about order of calling the constructor in inheritance. my question is even though the constructor and destructor are not inherited by derived class why the base class constructor will call when i create a derived class object. 回答1: The purpose of a constructor is to define how the data members should be initialised. Since a derived class inherits the data members from the base class, any constructor of the derived class must define not

Explicitly defaulted destructor disables default move constructor in a class

可紊 提交于 2021-02-05 07:10:29
问题 I have run into a problem that a move constructor of a superclass did not get invoked properly when its subclass has an explicitly defaulted destructor. The move constructor does get invoked when the destructor is implicitly defaulted (not provided at all in the supclass definition). I am aware of the constraints that the compilers should apply to default move constructors. Yet, I have been by all means sure that the compiler should not discriminate between explicitly/implicitly defaulted

Why is the destructor called more than the constructor? [duplicate]

谁说我不能喝 提交于 2021-02-04 17:29:27
问题 This question already has answers here : Two calls to destructor (3 answers) Closed 6 years ago . In the following code, the destructor is called twice, while the constructor is called only once: enum TFoo { VAL1, VAL2 }; class CFoo { public: TFoo mf; CFoo() { cout<<"hi c'tor1\n"; //mf = f; } CFoo(TFoo f) { cout<<"hi c'tor2\n"; mf = f; } CFoo(TFoo &f) { cout<<"hi c'tor3\n"; mf = f; } ~CFoo() { cout<<"bye\n"; } }; int main() { vector<CFoo> v; //v.assign(1, VAL1); v.push_back(VAL1); } The code

linux线程基本编程

你离开我真会死。 提交于 2021-02-01 04:02:30
索引: 1.创建线程pthread_create 2.等待线程结束pthread_join 3.分离线程pthread_detach 4.创建线程键pthread_key_create 5.删除线程键pthread_key_delete 6.设置线程数据pthread_setspecific 7.获取线程数据pthread_getspecific 8.获取线程标示符pthread_self 9.比较线程pthread_equal 10.一次执行pthread_once 11.出让执行权sched_yield 12.修改优先级pthread_setschedparam 13.获取优先级pthread_getschedparam 14.发送信号pthread_kill 15.设置线程掩码pthread_sigmask 16.终止线程pthread_exit 17.退出线程pthread_cancel 18.允许/禁止退出线程pthread_setcancelstate 19.设置退出类型pthread_setcanceltype 20.创建退出点pthread_testcancel 21.压入善后处理函数 22.弹出善后处理函数 -------------------------------------------------------------------------------

Call explicit constructor/destructor with traits in templatized function

跟風遠走 提交于 2021-01-28 19:59:51
问题 I'm trying to call explicit constructor/destructor with traits in templatized function. template <int i> struct Traits { }; template <> struct Traits<1> { typedef Foo type_t; }; template <> struct Traits<2> { typedef Bar type_t; }; template <class Traits> void DoSomething(void* p_in) { typename Traits::type_t* p = reinterpret_cast<typename Traits::type_t*>(p_in); // this works. new (p) typename Traits::type_t; // neither of following two does work. p->~typename Traits::type_t(); p->typename