initialization

Initialization of static class variable inside the main

核能气质少年 提交于 2021-02-04 13:36:08
问题 I have a static variable in the class. I am Initializing that in the global scope, its works fine. But When I try to Initialize in the main linker throws an error. Why it so. class Myclass{ static int iCount; } ; int main(){ int Myclass::iCount=1; } And In global scope why I have to specify the variable type like int Myclass::iCount=1; As In my class I am definig iCount as integer type why not. Myclass::iCount =1 ; in //Global scope 回答1: The section $9.4.2/7 from the C++ Standard says, Static

Static variable is initialized twice

让人想犯罪 __ 提交于 2021-02-04 13:17:07
问题 Consider I have a static variable in a compilation unit which ends up in a static library libA. I then have another compilation unit accessing this variable which ends up in a shared library libB.so (so libA must be linked into libB). Finally I have a main function also accessing the static variable from A directly and having a dependency to libB (so I link against libA and libB). I then observe, that the static variable is initialized twice, i.e. its constructor is run twice! This doesn't

Error when trying to use _init_ in Python 3.7.0 IDLE

我的梦境 提交于 2021-01-29 10:27:14
问题 So I was just trying to do same basic Classes stuff in Python.org's Python 3.7.0 IDLE (Shell), and when I tried to run this code: class Giraffes: def _init_(self, spots): self.giraffe_spots = spots gerald = Giraffes(100) It gave me this error: Traceback (most recent call last): File "<pyshell#69>", line 1, in <module> gerald = Giraffes(100) TypeError: Giraffes() takes no arguments Doesn't the init function in class Giraffes make Gerald take parameter self ? I'm just getting started with

Where to initialize static const member in c++17 or newer?

試著忘記壹切 提交于 2021-01-29 05:58:11
问题 The questions is where to initialize static const member in c++17 or newer? Please consider the following two solutions for initializing a static const member in c++: Solution 1 (for c++14 or older): //foo.h: #include <iostream> struct foo{ static const std::string ToBeInitialized; }; //foo.cpp #include <iostream> #include "foo.h" const std::string foo::ToBeInitialized{"with a value"}; Solution 2 (for c++17 or newer): //foo.h: #include <iostream> struct foo{ inline static const std::string

C++ is static variable initialization with = atomic?

北战南征 提交于 2021-01-29 03:59:02
问题 The Meyers Singleton depends on the fact that local static variable initialization is atomic. I'm building something similar but I want the singleton to actually be one of a number of derived types. The base class getInstance() method should call a factory that will return the appropriate type. My first idea is static Foo *instance = FooFactory(...); 8.5p2 of N3337 seems to indicate that this is strictly initialization and not initialization and assignment and I interpret that to mean the

Possible to initialize/assign a struct pointer?

自作多情 提交于 2021-01-28 11:53:50
问题 Let's say I have the following struct and two versions to initialize it: #include <stdio.h> typedef struct Car *CarPtr; typedef struct Car { const char* name; unsigned int price; } Car; int main(void) { Car ford = { .name = "Ford F-150", .price = 25000 }; print_struct(&ford); // is this possible to do in a single assignment? CarPtr jeep = { .name = "Jeep", .price = 40000 }; print_struct(jeep); } Is the second version possible to do directly? Or do I need to do something along the lines of:

crosses initialization error in switch case statement

泪湿孤枕 提交于 2021-01-28 07:02:21
问题 I've got the following code: Class A { public: A::A(const char* name): _name(name) {} virtual void doSomething(); private: const char* _name; } Class B : public A { B::B(const char* name): A(name) {} void doSomething() { //do something } } So far so good, but I've experiencing an error crosses initialization of B* newB in the following code: std::vector<A*> vectorOfAs; switch (enumType) { case enum1 : B* newB = new B("foobar"); vectorOfAs.push_back(newB); break; case enum2 : C* newC = new C(

Detecting shadowing of member variables

旧街凉风 提交于 2021-01-28 06:49:29
问题 I have a class A with a member: std::shared_ptr<class B> m_mySharedPtr; I have an Init method: A::Init() { std::shared_ptr<class B> m_mySharedPtr = m_factory->CreateMySharedPtr(); } It took me some time to realize that after my Init the shared_ptr is Empty. That is because I re-define the member, treated as a local and thus released when out of A::Init() scope. I meant to write: A::Init() { m_mySharedPtr = m_factory->CreateMySharedPtr(); } Why did the compiler not complain? At least a warning

Initialising with std::call_once() in a multithreading environment [duplicate]

霸气de小男生 提交于 2021-01-28 06:32:54
问题 This question already has an answer here : Is std::call_once a blocking call? (1 answer) Closed 1 year ago . I'm reading the book C++ Concurrency in Action, 2nd Edition X . The book contains an example that uses the std::call_once() function template together with an std::once_flag object to provide some kind of lazy initialisation in thread-safe way. Here a simplified excerpt from the book: class X { public: X(const connection_details& details): connection_details_{details} {} void send_data

What's the difference between parentheses and braces in c++ when constructing objects

笑着哭i 提交于 2021-01-28 05:40:17
问题 What's the difference between () and {} when constructing objects? I think {} should only support with initializer_list or an array, but when I run below snip, I confused. #include <iostream> using namespace std; struct S { int v=0; S(int l) : v(l) { } }; int main() { S s1(12); // statement1 S s2{12}; // statement2 cout << s1.v << endl; cout << s2.v << endl; } statement1 is right because () is the basic grammar for constructing the object. I expect the statement2 will be compiled failed. I