constructor

Inherit constructors from template base class without repeating template arguments?

怎甘沉沦 提交于 2020-01-21 06:54:51
问题 How do I inherit constructors from a template base class without repeating the template arguments (and without using macros): For example, this does not work (using GCC 4.8): template <typename T> struct base {}; template <typename U> struct derived : base<U> { using base::base; }; It does work if I repeat the template arguments of the base class: template <typename T> struct base {}; template <typename U> struct derived : base<U> { using base<U>::base; }; The problem is that "U" might be

Java - Inner class constructor - allowed for outer class only

一个人想着一个人 提交于 2020-01-21 06:20:16
问题 I have inner class in my code. I want to give public access to its instances, but only outer class should be able to create this instances, like in "private" access. Is it possible without making properly small package (or creating public interface for every such inner class)? (Sorry if my english is bad :P) 回答1: It is possible. Declare your inner class public, but its constructor private . This way you can create it only inside your enclosing class and itself, but not from outside. 回答2: By

C++ std::ifstream in constructor problem

房东的猫 提交于 2020-01-21 05:12:38
问题 I've got a problem with this code: #include <fstream> struct A { A(std::ifstream input) { //some actions } }; int main() { std::ifstream input("somefile.xxx"); while (input.good()) { A(input); } return 0; } G++ outputs me this: $ g++ file.cpp file.cpp: In function `int main()': file.cpp:17: error: no matching function for call to `A::A()' file.cpp:4: note: candidates are: A::A(const A&) file.cpp:6: note: A::A(std::ifstream) After changing it to this it compile (but that is not solving the

c++ Constructor initializer list with complex assignments

久未见 提交于 2020-01-20 08:06:33
问题 Suppose I want to have a constructor that receives some parameters, and with these parameters I can calculate the values for it's member variables. Except that the values for the member variables are not simple assignments from the parameters. They require creation of other objects and transformation of the values before they can be used as values for the member variables. This is way to much to cram into an initializer list. Also very inefficient since you can't create variables and reuse

Calling a constructor of the base class from a subclass' constructor body

匆匆过客 提交于 2020-01-20 04:05:11
问题 I was under impression that it's impossible, see for example: Calling the constructor of the base class after some other instructions in C++ But the following program runs and produces two lines of "Constructor Person": #include <iostream> class Person { public: Person() { std::cout << "Constructor Person" << std::endl; } }; class Child : public Person { public: Child() { c = 1; Person(); } int c; }; int main() { Child child; return 0; } The first one is implicit call of the default

Pass std::list to constructor using boost's list_of doesn't compile

梦想的初衷 提交于 2020-01-17 07:36:07
问题 I am trying to do this: class bbb { public: bbb(std::list<int> lst) { } }; int main() { bbb b((std::list<int>)boost::assign::list_of<int>(10)(10)); return 0; } and get following error from g++: some.cc:35: error: call of overloaded 'bbb(boost::assign_detail::generic_list<int>&)' is ambiguous some.cc:15: note: candidates are: bbb::bbb(std::list<int, std::allocator<int> >) some.cc:13: note: bbb::bbb(const bbb&) Is there any way to work around this problem? Note that I am using gcc 4.4.6. It

Checkboxes not showing up

人走茶凉 提交于 2020-01-16 14:54:43
问题 I am hoping another set of eyes can help me find where my code is wrong. I can compile and run the program, but all I get is a white screen. It's supposed to show the checkboxes and change the background to whichever color is chosen. Any help is appreciated! import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Chapter5Debug extends Frame implements ItemListener { static Chapter5Debug f = new Chapter5Debug(); CheckboxGroup options = new CheckboxGroup();

How to initialize a large private array in a class using constructor(just a constructor) in c++? [duplicate]

随声附和 提交于 2020-01-16 12:02:51
问题 This question already has answers here : Zero-Initialize array member in initialization list (3 answers) C++ Initializing Non-Static Member Array (9 answers) How to initialize all members of an array to the same value? (22 answers) Closed yesterday . Suppose we have a class like the following one: class myprogram { public: myprogram (); private: double aa,bb,cc;}; myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){} As you can see we can initialize our private members' aa, bb, cc using the

How to initialize a large private array in a class using constructor(just a constructor) in c++? [duplicate]

自古美人都是妖i 提交于 2020-01-16 12:01:11
问题 This question already has answers here : Zero-Initialize array member in initialization list (3 answers) C++ Initializing Non-Static Member Array (9 answers) How to initialize all members of an array to the same value? (22 answers) Closed yesterday . Suppose we have a class like the following one: class myprogram { public: myprogram (); private: double aa,bb,cc;}; myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){} As you can see we can initialize our private members' aa, bb, cc using the

Order of initialization/instantiation of class variables of derived class and invoking of base class constructor

醉酒当歌 提交于 2020-01-16 05:34:05
问题 I want to figure out the order of 1) initialization/instatiation of derived class variables 2) invoking of base class constructor in this code snippet public class base { int y = 1; public base() { y = 2; function(); } void function () { System.out.println("In base Value = " + String.valueOf(y)); } public static class derived extends base { int y = 3; public derived() { function(); } void function () { System.out.println("In derived Value = " + String.valueOf(y)); } } public static void main