member

C++ Call a non-virtual non-static method from a null pointer, without accessing members : is this guaranteed to work? [duplicate]

匆匆过客 提交于 2019-12-24 11:55:42
问题 This question already has answers here : Is it legal/well-defined C++ to call a non-static method that doesn't access members through a null pointer? (5 answers) When does invoking a member function on a null instance result in undefined behavior? (2 answers) Closed 5 years ago . Can I call a non-static, non-virtual method of a class from a null pointer? The member function would then test if this==nullptr, and return immediately if it's true. I know it will work in most cases, but is this a

Why do class member functions defined outside the class (but in header file) have to be inlined?

狂风中的少年 提交于 2019-12-24 01:45:54
问题 I have read existing answers on the two meanings of inline, but I am still confused. Let's assume we have the following header file: // myclass.h #ifndef INCLUDED_MYCLASS #define INCLUDED_MYCLASS class MyClass { public: void foo(); // declaration }; inline void MyClass::foo() { // definition } #endif Why does void foo() which is defined outside the class in the file, have to be explicitly defined with inline ? 回答1: It's because you defined MyClass::foo in a header file. Or a bit more abstract

Lambda expression as member functors in a class

烂漫一生 提交于 2019-12-24 00:35:08
问题 I was thrilled when lambda expressions (LE) were part of the gcc starting a 4.5.1 and hoped they would grant a way of getting rid of those nasty functions pointer in C++, which were basically, to my understanding, compiled as C functions. All those static declarations etc... Now I wanted to use LEs in a class, where one can choose a method of computation by a functor. But due to the definition in the proposal for C++1x, this seems not to be possible at all. Here the code and the problem(s).

c++ defining a static member of a template class with type inner class pointer

徘徊边缘 提交于 2019-12-23 19:14:34
问题 I have a template class like here (in a header) with a inner class and a static member of type pointer to inner class template <class t> class outer { class inner { int a; }; static inner *m; }; template <class t> outer <t>::inner *outer <t>::m; when i want to define that static member i says "error: expected constructor, destructor, or type conversion before '*' token" on the last line (mingw32-g++ 3.4.5) 回答1: You need to qualify that the inner class is a typename , since it’s dependent on a

C++ To call member function in for_each for items in the member container

徘徊边缘 提交于 2019-12-23 12:09:51
问题 If I have a class (that mimic some of STL's container) like this: class Elem { public: void prepare(); // do something on *this // ... }; class Selector { public: typedef vector<Elem *> container_type; typedef container_type::iterator iterator; iterator begin() { return cont_.begin(); } iterator end() { return cont_.end(); } void check_all(); private: prepare_elem(Elem *p); // do something on 'p' container_type cont_; }; If I want to call prepare() for all elements in 'cont_', I could make

c++ memory in array of class objects

梦想的初衷 提交于 2019-12-23 11:56:21
问题 I have a class like this: class Object { public: unsigned char data[8]; // other variables // functions etc... }; The question is - are the object members all stored in the same place in memory relative to the object? So if I have an array: Object array[3], given a char pointer char* data_ptr = array[0].data , will data_ptr + (sizeof(Object)) then always point to array[1].data? (I've read a couple of Q/As about how there might be padding inbetween data members of classes and structs - but I

Does an element exists in a list of lists?

北慕城南 提交于 2019-12-23 06:57:30
问题 I want to find if a given element exists in a list of lists. I am only getting true if the element exists somewhere is the first list of lists. Any advice? memberlist(X,[[X|T1]|T2]). memberlist(X,[[H|T1]|T2]) :- memberlist(X,[T1|T2]). 回答1: The problem is that [[H|T1]|T2] only matches given the first list has at least one element. Indeed: [[],[1,4],[2,5]] for instance does not unify with [[H|T1]|T2] . So you can solve the problem by adding a clause: memberlist(X,[[]|T2]) :- memberlist(X,T2).

Is 'x?.y' in Swift the same as 'x?' followed by '.y'?

喜你入骨 提交于 2019-12-23 04:52:36
问题 I understand that in Swift, if I define var opt:String? = "Optional" I will get an error if I attempt opt.isEmpty since opt is of type String? which does does not have an isEmpty method. And I thought that I understood that opt?.isEmpty does not produce an error because opt? unwraps (any non- nil ) opt , resulting in a String , which does have an isEmpty method. But opt? on its own results in a String? and not a String . Is ?. a different operator from ? followed by . ? Try (opt?).isEmpty and

Qt: Different tableWidget member names on workstations

谁说我不能喝 提交于 2019-12-22 20:54:33
问题 I am using Qt on two ubuntu machines and am copying the source code from time to time between them. I found a really annoying problem when doing that and I can't figure out why this happens. I am using a table Widget to display some data and want to stretch the horizontal header to fit the content length. To do that I use the following line: ui->tableWidget->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents); This works just fine. I have a few of this codelines. However, when

Qt: Different tableWidget member names on workstations

一个人想着一个人 提交于 2019-12-22 20:53:14
问题 I am using Qt on two ubuntu machines and am copying the source code from time to time between them. I found a really annoying problem when doing that and I can't figure out why this happens. I am using a table Widget to display some data and want to stretch the horizontal header to fit the content length. To do that I use the following line: ui->tableWidget->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents); This works just fine. I have a few of this codelines. However, when