static-members Questions

2

Solved

I'd like to have a base class that has a constant field (like an unique ID associated with the class that can't be modified after compile time). So far the static const declaration would be just fi...
Aetolia asked 6/6, 2012 at 13:50

6

Solved

void foo (int x) { struct A { static const int d = 0; }; // error } Other than the reference from standard, is there any motivation behind this to disallow static field inside an inner class ? ...
Mayo asked 26/5, 2011 at 10:48

1

Solved

Let's consider a code header: class uid { public: uid () {++i; } static int i; }; class foo { public: const static uid id; } source: static int uid::i = 0; The header could be included ...
Amaryllidaceous asked 13/5, 2020 at 18:13

6

Solved

Either I'm very tired or something weird is happening that I'm not aware of, because the code below is resulting in undefined symbols for Foo::A and Foo::B when linking. This is minimized as much a...

2

Solved

I know that the question has been asked several times and I've been reading posts like: Initializing static members of a templated class How can I Declare/define/initialize a static member variab...
Esquiline asked 27/10, 2019 at 20:21

5

Solved

Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include <iostream> class demo { public: static void fun() { st...
Forficate asked 12/2, 2015 at 16:34

3

We have two classifications heap and stack . When a object is created, memory for object is stored in heap. What if the class has static methods ,which can be called using class name. If object is ...
Gherlein asked 6/10, 2011 at 11:2

5

Solved

I have a class class foo { public: foo(); foo( int ); private: static const string s; }; Where is the best place to initialize the string s in the source file?
Mcleroy asked 9/4, 2010 at 6:40

6

Solved

I want to have a static const char array in my class. GCC complained and told me I should use constexpr, although now it's telling me it's an undefined reference. If I make the array a non-member t...
Hookah asked 4/11, 2011 at 23:11

5

I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this...
Pachydermatous asked 28/10, 2015 at 9:29

0

This one's a little weird/complex and more just curiosity than anything. I was looking for a way to make sure static calls from a base class could safely use static information set up in a derived...
Dopester asked 21/3, 2019 at 22:12

7

Solved

Given is a class with a static member. class BaseClass { public: static std::string bstring; }; String has obviously to be default-initialized outside of the class. std::string BaseClass...
Guzzle asked 17/9, 2013 at 22:25

2

Solved

Given the following code: #include <iostream> template <std::size_t N> struct foo { static std::size_t value; }; template <> std::size_t foo<0>::value = 0u; template &l...
Sulfonmethane asked 4/3, 2019 at 11:7

3

Solved

What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i...
Cordovan asked 29/7, 2012 at 14:6

2

Solved

I have an expensive function like this: pub fn get_expensive_value(n: u64): u64 { let ret = 0; for 0 .. n { // expensive stuff } ret } And it gets called very frequently with the same argumen...
Wadi asked 26/3, 2016 at 2:16

1

Solved

struct sa { struct sb { int a = 123;}; inline static sb b; }; The above code generates an error: main.cpp:25:20: error: default member initializer for ‘sa::sb::a’ required before the end of it...
Sweepstakes asked 22/8, 2018 at 2:32

11

Solved

I'd like to have a private static constant for a class (in this case a shape-factory). I'd like to have something of the sort. class A { private: static const string RECTANGLE = "rectangle&q...
Abseil asked 14/10, 2009 at 2:0

2

Solved

Is the following code valid, e.g. doesn't bring undefined behaviour? struct S { int i = s.i; static S s; }; S S::s; int main() { S a; // a.i = 0 S::s.i = 42; S b; // b.i = 42 } As far as ...

3

Solved

I'd like to do this: template <typename T> struct S { ... static double something_relevant = 1.5; }; but I can't since something_relevant is not of integral type. It doesn't depend on T,...
Selden asked 12/7, 2010 at 15:45

6

Solved

Given an hypothetical utility class that is used only in program setup: class MyUtils { private static MyObject myObject = new MyObject(); /*package*/static boolean doStuff(Params... params) { ...
Billiards asked 17/1, 2009 at 9:2

9

Solved

I just did a little experiment: public abstract class MyClass { private static int myInt = 0; public static int Foo() { return myInt; } public static int Foo(int n) { myInt = n; return ...
Diahann asked 13/5, 2011 at 0:31

3

Solved

I am trying to access a static member from a non-static function in the class, and I get an error saying Static member cannot be accessed off an instance variable this is how my code looks - ...
Oballa asked 19/3, 2014 at 19:33

7

Solved

I've recently made a change stylistically and wanted to see how other c++ programmers felt about it and if there were any downsides to it. Essentially, when I needed a utility function which doesn...
Smoking asked 6/7, 2011 at 14:40

4

Solved

I am new to C++. I have a class like this: class CodeTest { private: static const int TOTAL=100; }; Is TOTAL a declaration or a definition? When I was reading Scott Meyer's book, it was mentio...
Gainor asked 24/6, 2012 at 14:52

2

Solved

I'm learning C++. The documentation learn.microsoft.com/en-us/cpp/cpp/member-access-control-cpp says: When you specify a base class as private, it affects only nonstatic members. Public static m...
Logistic asked 27/11, 2017 at 8:55

© 2022 - 2025 — McMap. All rights reserved.