constructor

Expected unqualified-id before ')' token? [closed]

Deadly 提交于 2020-06-18 11:45:33
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Improve this question I want to create a base class and get an error with my initializer list in default constructor. Here are the errors I'm getting: giasuc.cpp:3:8: error: expected unqualified-id before ')' token GiaSuc(): ten(""), soConSinh(0), soLitSua(0) { ^ giasuc.cpp:6:14:

Python class constructor (static)

痞子三分冷 提交于 2020-06-18 10:54:35
问题 Does Python have a mechanism for class constructors, i.e. a function that is called whenever the class is first referenced (as opposed to when an instance of that object is created)? I know this exists in some other languages, but I haven't come across it in Python. Basically, I would like to initialise some static attributes in that function. I put an example below of what I would expect. Of course, the example returns None, but I would like it return 'foo'. class T: arg = None def __class

What's the most idotomatic way of declaring two constructors with the same args?

て烟熏妆下的殇ゞ 提交于 2020-06-16 16:59:13
问题 As an example lets say I have a class called File . Now file can be opened as binary or text. My constructor is currently File(const char*filename) . Let's suppose the implementation of open is completely different binary and text. How the heck do I construct this? I thought about using a static function but I don't want to return a pointer. I could pass in a pointer but I rather not allow a class be constructed without actually initializing it. I was thinking about having an enum or bool in

Alignment of a simple class to allow array access without UB

情到浓时终转凉″ 提交于 2020-06-13 07:54:50
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Alignment of a simple class to allow array access without UB

不想你离开。 提交于 2020-06-13 07:54:08
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

Alignment of a simple class to allow array access without UB

牧云@^-^@ 提交于 2020-06-13 07:53:47
问题 Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} }; Since I want array-like access to the name member of employee in an array of employees, I need for example that the offsets are divisible: static_assert( sizeof(employee) % sizeof(std::string) == 0, "!" ); In order to ensure that I am using the alignas directive: struct alignas(sizeof

How to create a constructor that is only usable by a specific class. (C++ Friend equivalent in c#)

拟墨画扇 提交于 2020-06-11 16:53:13
问题 As far as I know, in C#, there is no support for the "friend" key word as in C++. Is there an alternative way to design a class that could achieve this same end result without resorting to the un-available "friend" key-word? For those who don't already know, the Friend key word allows the programmer to specify that a member of class "X" can be accessed and used only by class "Y". But to any other class the member appears private so they cannot be accessed. Class "Y" does not have to inherit

C++ Constructor: Perfect forwarding and overload

浪尽此生 提交于 2020-06-10 04:39:14
问题 I have two classes, A and B, and B is derived from A. A has multiple constructors (2 in the example below). B has one additional member to initialize (which has a default initializer). How can I achieve that B can be construced using one of the constructors of A without having to manually rewrite all the constructor overloads from A in B? (In the example below, I would otherwise have to provide four constructors for B: B():A(){} , B(string s):A(s){} , B(int b):A(),p(b){} , B(string s, int b)

C++ Constructor: Perfect forwarding and overload

六眼飞鱼酱① 提交于 2020-06-10 04:39:07
问题 I have two classes, A and B, and B is derived from A. A has multiple constructors (2 in the example below). B has one additional member to initialize (which has a default initializer). How can I achieve that B can be construced using one of the constructors of A without having to manually rewrite all the constructor overloads from A in B? (In the example below, I would otherwise have to provide four constructors for B: B():A(){} , B(string s):A(s){} , B(int b):A(),p(b){} , B(string s, int b)

How to unit test the methods of a class whose constructor take some arguments?

匆匆过客 提交于 2020-05-26 01:22:31
问题 I have a class of form something like this: class A{ public function __constructor(classB b , classC c){ // } public function getSum(var1, var2){ return var1+var2; } } My test case class is something like this: use A; class ATest extends PHPUnit_Framework_TestCase{ public function testGetSum{ $a = new A(); $this->assertEquals(3, $a->getSum(1,2)); } } However when I run the phpunit, it throws some error like: Missing argument 1 for \..\::__construct(), called in /../A.php on line 5 Even if I