constructor

Assignment operator in linked list C++

允我心安 提交于 2020-01-13 06:44:07
问题 I'm trying to implement linked list in c++. I implement my assignment operator like this: // assignment operator template<class T> LinkedList<T>& LinkedList<T>::operator = (const LinkedList& rhs) { if (&rhs != this) { Node *tmp = head; while (tmp -> next) { head = head -> next; delete tmp; tmp = head; } tmp = rhs -> head; while (tmp) { append(tmp); tmp = tmp -> next; } } return *this; } In my main function, i use the following code to test: LinkedList<int> *lst1 = new LinkedList<int>(7);

Initializing lists in a constructor

主宰稳场 提交于 2020-01-13 05:58:07
问题 I need to create a class that also initializes two event lists to new empty lists. I'm not sure if that is what is being asked of me, but I know how to create a list and how to create a constructor. I created 2 lists, and now I should create the constructor. Here is one of my lists: List<Person> organize = new List<Person>(); How do I initialize the two event lists in the constructor to new lists? 回答1: Based on what I can gather from your question, you have a class with two Lists. Your

Initializing lists in a constructor

∥☆過路亽.° 提交于 2020-01-13 05:58:04
问题 I need to create a class that also initializes two event lists to new empty lists. I'm not sure if that is what is being asked of me, but I know how to create a list and how to create a constructor. I created 2 lists, and now I should create the constructor. Here is one of my lists: List<Person> organize = new List<Person>(); How do I initialize the two event lists in the constructor to new lists? 回答1: Based on what I can gather from your question, you have a class with two Lists. Your

C# - Chain Calling of Constructor

痞子三分冷 提交于 2020-01-13 05:17:48
问题 I'm currently studying C# and I'm learning about constructors and the chain calling of constructors so as not to have to paste the same code (the same value for variables) in each constructor. I have three constructors, one with no parameters, one with three parameters and one with four parameters. What I'm looking to do is, use the default constructor to call the constructor with three parameters, passing default values for the parameters (variables) and the constructor that has three

PHP Setters/Getters and Constructor

眉间皱痕 提交于 2020-01-12 19:36:10
问题 I have been searching for this online, but I can't seem to find something that is clear enough for me to understand. I have seen "similiar" questions on here about this in Java. class animal{ private $name; // traditional setters and getters public function setName($name){ $this->name = $name; } public function getName(){ return $this->name; } // animal constructors function __construct(){ // some code here } // vs function __construct($name){ $this->name = $name; echo $this->name; } } $dog =

Input checks in Haskell data constructors

北慕城南 提交于 2020-01-12 13:48:08
问题 How can I add input checks to Haskell data constructors? Let's say I have import Data.Time.Calendar data SchedulePeriod = SchedulePeriod { startDate :: Day , endDate :: Day , accrualStart :: Day , accrualEnd :: Day , resetDate :: Day , paymentDate :: Day , fraction :: Double } deriving (Show) and I want to impose a constraint startDate < endDate . Is there a way to do it without creating an abstract data type? 回答1: The standard way is to use a smart constructor that checks the precondition

What's the best practice to prevent memory leak if an exception thrown in constructor?

旧街凉风 提交于 2020-01-12 04:41:33
问题 I know if an exception is thrown in constructor, destructor will not be called(simple class, no inheritance). So if an exception is thrown in constructor and there is a chance some heap memory is not cleaned up. So what's best practice here? let's assume I have to call some function in constructor and it may throw exception. Shall I always use shared pointer in this case? What's the alternatives? Thank you! 回答1: I would stick to the RAII idiom. If you avoid "naked" resources (like operator

Getting error: ISO C++ forbids declaration of with no type

有些话、适合烂在心里 提交于 2020-01-12 03:57:06
问题 I'm getting the following errors: ISO C++ forbids declaration of ttTreeInsert with no type ISO C++ forbids declaration of ttTreeDelete with no type ISO C++ forbids declaration of ttTreePrint with no type prototype for int ttTree::ttTreePrint() does not match any in class ttTree candidate is: void ttTree::ttTreePrint() Here is my header file: #ifndef ttTree_h #define ttTree_h class ttTree { public: ttTree(void); int ttTreeInsert(int value); int ttTreeDelete(int value); void ttTreePrint(void);

Java Lombok: Omitting one field in @AllArgsConstructor?

三世轮回 提交于 2020-01-12 03:55:07
问题 If I specify @AllArgsConstructor using Lombok , it will generate a constructor for setting all the declared (not final, not static) fields. Is it possible to omit some field and this leave generated constructor for all other fields? 回答1: No that is not possible. There is a feature request to create a @SomeArgsConstructor where you can specify a list of involved fields. Full disclosure: I am one of the core Project Lombok developers. 回答2: Alternatively you could use @RequiredArgsConstructor .

Is the “iframe sandbox” technique safe?

拜拜、爱过 提交于 2020-01-11 20:58:49
问题 Update: Since this is unanswered, I'm changing the question slightly. Comments on the post on Dean's blog linked below indicate this technique does not work in Safari. My question now is: does the technique described below work* in modern browsers, and in particular can someone confirm whether it works in Safari? Here's a more recent blog post. It says at one point: Sandboxed natives ... are supported in a variety of browsers, including ... Safari 2.0+ ...but later says that the iframe