constructor

How delegate from copy constructor to universal copy constructor template?

元气小坏坏 提交于 2020-01-14 03:55:09
问题 If I want to write a universal copy constructor (one that will take any argument type), it's easy enough to do: class Widget { public: template<typename T> Widget(T&& other); }; This won't prevent the compiler from generating a traditional copy constructor, so I'd like to write that myself and delegate to the template. But how do I do that? class Widget { public: template<typename T> Widget(T&& other); Widget(const Widget& other): ??? {} // how delegate to the template? }; I tried to write

Cannot set visibility or call Show, ShowDialog or EnsureHandle after a window has been closed

百般思念 提交于 2020-01-14 02:51:29
问题 This is my App constructor for my WPF application: public partial class App { public App() { Run(new Login(false)); } } And this is my Login constructor: public Login(bool ignoreSettings) { InitializeComponent(); if (ignoreSettings) { TxtUsername.Text = SettingsSaver.LoadUsername(); TxtCrmUrl.Text = SettingsSaver.LoadCrmUrl(); return; } if (string.IsNullOrWhiteSpace(SettingsSaver.LoadUsername()) || string.IsNullOrWhiteSpace(SettingsSaver.LoadCrmUrl())) return; try { CrmConnector.ConnectToCrm(

manual object constructor call

亡梦爱人 提交于 2020-01-13 11:29:48
问题 Can you please tell me if it is possible to call object constructor manually ? I know it's wrong and I would never do something like that in my own code and I know I can fix this problem by creating and calling initialization function, however the problem is that I stumbled at a case where there are thousands of lines of code in object's and its parents' constructors... class MyClass() { MyClass() { } virtual ~MyClass(); void reset() { this->~MyClass(); this->MyClass::MyClass(); //error:

Does a Java constructor return the Object reference?

99封情书 提交于 2020-01-13 10:33:14
问题 I know Java's constructors can't have any type and interestingly it cannot even be void . A logical explanation for that would be that a constructor returns the initialized object's reference. MyClass myObject = new MyClass(); The constructor of myClass will now return the object reference after instantiating it and save it in the object variable MyObject and that's why the constructor can't have a return type. Is that right? Could someone confirm this? 回答1: No, actually, the constructors are

Why can't constructor be declared as static in c++?

前提是你 提交于 2020-01-13 08:53:15
问题 I have recently finished reading the 1st Vol. of Thinking in C++ by Bruce Eckel and have now turned to applying the knowledge to some practical use. I was recently working with static member functions and tried making the constructor static, for which the compiler was unhappy. I checked for the reason in the book but couldn't find any. Can anyone explain why? P.S.: After seeing some responses, I would like to mention that the confusion arises from my knowledge that C# (and Java) allows

Why can't constructor be declared as static in c++?

南笙酒味 提交于 2020-01-13 08:53:06
问题 I have recently finished reading the 1st Vol. of Thinking in C++ by Bruce Eckel and have now turned to applying the knowledge to some practical use. I was recently working with static member functions and tried making the constructor static, for which the compiler was unhappy. I checked for the reason in the book but couldn't find any. Can anyone explain why? P.S.: After seeing some responses, I would like to mention that the confusion arises from my knowledge that C# (and Java) allows

Constructor injection with other, non-dependency, constructor arguments

心已入冬 提交于 2020-01-13 08:19:41
问题 I'm new to IOC containers, and I'm getting started with NInject. What do you do if you want your constructor to have parameters that are not services and don't need to be instantiated by the IOC container? For example: public class Person { private readonly string _name; private readonly IPersonRepository _repository; public Person(string name, IPersonRepository repository) { _name = name; _repository = repository; } ...... } Imagine that name is a requirement of the Person class, so, to

F# Object Initialization with a Constructor

北城余情 提交于 2020-01-13 08:15:11
问题 I know that in F# if you have a C# class of the format: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } You can initialize it like so, which is nice: let p = new Person (Name = "John", BirthDate = DateTime.Now) However how would you initialize it in F# if the C# class also had a constructor like this: public class Person { public Person(int id) { Id = id } public int Id {get; private set;} public DateTime BirthDate { get; set; } public string

Call default copy constructor from within overloaded copy constructor

给你一囗甜甜゛ 提交于 2020-01-13 07:55:45
问题 I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo

Move constructor/operator=

时间秒杀一切 提交于 2020-01-13 07:21:10
问题 I'm trying to learn about new feature of C++ namely move constructor and assignment X::operator=(X&&) and I found interesting example but the only thing I quite not even understand but more dissagree is one line in move ctor and assignment operator (marked in the code below): MemoryBlock(MemoryBlock&& other) : _data(NULL) , _length(0) { std::cout << "In MemoryBlock(MemoryBlock&&). length = " << other._length << ". Moving resource." << std::endl; // Copy the data pointer and its length from