Questions tagged [in-class-initialization]

55 questions
2
votes
2 answers

How can I set field value before class initialization?

I am reading source code about the java.sql.DriverManager,and found something confusing.Here is the code: static { loadInitialDrivers(); println("JDBC DriverManager initialized"); } ...... public static void println(String message) { …
Jack Zhou
  • 21
  • 2
2
votes
2 answers

c++ initialize class member variable depends on other member variable

Basically, a non-static member theta which initialize by another class member but well initialized. Then valley_max initialized by theta as you can see. Right now everything works fine. Then I want to initialize a array whose bound is valley_max.…
crazymumu
  • 101
  • 2
  • 10
2
votes
0 answers

Why const char* static field has to be constexpr to initialize it inside class?

I have the following piece of code: struct st { static constexpr const int x = 2; static constexpr int x2 = 2; static const int x3 = 2; static const char* str = "BLAH"; // ERROR }; marked line gives me the following error: error:…
Patryk
  • 22,602
  • 44
  • 128
  • 244
2
votes
2 answers

In-class member initializer using a constructor: is it allowed?

I recently found an interesting piece of code in the article Get to Know the New C++11 Initialization Forms by Danny Kalev: class C { string s("abc"); double d=0; char * p {nullptr}; int y[5] {1,2,3,4}; public: C(); }; The line string s("abc");…
Constructor
  • 7,273
  • 2
  • 24
  • 66
1
vote
0 answers

Chicken and egg problem in Java class initialization

Say we have this class: public class Chicken { public static final Chicken THE_FIRST_ONE = new Chicken("Little"); public static final Chicken THE_FIRST_EGG_1 = new Egg("Humpty-Dumpty"); public static final Egg THE_FIRST_EGG_2 =…
Gunther Schadow
  • 1,490
  • 13
  • 22
1
vote
3 answers

Why we need a separate definition for a static const data member?

Hello if I have a static const data member then I can provide an in-class initializer for it and I don't need to define it again outside of the class body. But that is true only if that constant is used within the class scope and if used outside, a…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
1
vote
3 answers

During instantiation of class member: "no operator "[]" matches these operands" when accessing std::map value with map[key]

I have been looking at this for quite some time. I don't understand what is bothering it. The minimum code that is giving me the problem is shown bellow. I comment on the line where the error occurs. Compilation errors follow after the…
Pana
  • 121
  • 7
1
vote
0 answers

what is the use of TestIntialize attribute, when i can do everything in ClassIntialize in MS testing?

[ClassInitialize] public static void ReportClassSetup(TestContext context) { BaseAdapter.ConnectionString = ConfigurationManager.ConnectionStrings["PremierConfig"].ConnectionString; IServiceContext serviceContext = new…
Sparrow
  • 355
  • 4
  • 19
1
vote
0 answers

static constexpr member in-class initialization

Could anyone help me find out what is wrong with in-class initialization of static constexpr member varaible like in the code below ? Using Visual Studio 2013 struct hg { public: static constexpr float asd = 9.0f; }; int main() { return…
1
vote
1 answer

Python: multiple inheritance, copy constructor, class initialization and () overloading

I was looking for a way to initialise the derived class using copy constructor and () operator like in C++ class Rectangle { int width, height; public: Rectangle (int,int); int area () {return (width*height);} }; Rectangle::Rectangle…
1
vote
1 answer

In class default initializer for array member C++11

How do I default initialize a member array in C++11? It seems that I have to provide a bound. class Example { const char* num2letter[10]{" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; }; Compiles fine. But if I omit the…
Rich
  • 1,669
  • 2
  • 19
  • 32
1
vote
2 answers

Named Parameter Idiom using a pointer to a class private method

I got stuck with a C++ compilation error while doing something that is probably not really "conventional". To make things easier I just re-wrote the mechanism I am trying to use in a easier-to-read way and I checked that I got the same issue. First…
0
votes
3 answers

Different behavior of __init__ method depending on order in code

I have created a unit class to manage units of physical quantities the user will input with their corresponding numerical values. The unit class takes a string input, and saves data in the form of three arrays containing each the physical units,…
MyDoom
  • 37
  • 1
  • 6
0
votes
2 answers

Function was put into a curly bracket in order to initialize the member in class. What is its syntax?

The code is shown here: class Basket{ public: /*other contents*/ private: // function to compare shared_ptrs needed by the multiset member static bool compare(const std::shared_ptr &lhs, const std::shared_ptr &rhs) …
0
votes
1 answer

Using external functions to create the attributes of a class object always returning same values

During the process of making a simple football game, I have run across an issue I have not had before with class object initialisation, and can't find a good solution. As seen in the main method at the bottom of the code, iteratively creating new…