I have a class that extends another class, and I want multiple constructors in the child class, but the child constructor needs to call the parent constructor. This is what I have
ChildClass child1; // do stuff with child1 // breaks ChildClass child2(1); // do stuff with child2 // works fine
Using the default constructor breaks my program at runtime, but using the one with a parameter works fine. The default constructor calls the other with the same thing as the main part in the program, so I would think this should make no difference, but obviously that isn't the case.
I am currently having an issue with a piece of code that I am writing in which I need to use a vector of a child class as a parameter in a function in the parent class. Below is an example of my code:
#include "child.h" #include <vector> class parent { parent(); function(std::vector<child> children); // rest of class here }
When I do this my program doesn't compile. However if I try to forward declare, as shown in the following example, it once again refuses to compile:
#include <vector> class child; class parent{ parent(); function(std::vector<child> children); // rest of class here }
This time, it refuses to compile because it needs to know the full size of the class child in order to create the vector. How to being able to access the child is essential for my program, so what should I do?
Constructor of the Base Class Person::Person(char* n="", char* nat="U.S.A", int s=1) { name = n; nationality = nat; sex = s; }
Constructor of the Derived Class (inherited from the base class)
Student(char* n, int s=0, char* i=""): Person(n, s)
Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?
Below is simplified code consists of two classes, namely Parent and Child.
Child is inherited from Parent.
All member functions of class Parent are declared virtual, and they have been overridden in the class Child.
Code 1:
#include <cstdlib> #include <iostream> using namespace std; #define QUANTITY 5 class Parent {
[Code] ....
The output of the code:
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1681692777 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Parent::operator=() invoked.
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Question:
Why is Parent::operator= invoked instead of Child::operator= ..?
Isn't it already declared virtual and hence would be overridden..?
I need to invoke Child::operator= instead. How to achieve this?
I am doing C++ data structures exercises, and I am still learning some of the more basic concepts. I have a parent class:
template<class T> class linkedListType { public: protected: int count; nodeType<T> *first; nodeType<T> *last; private: };
Derived class:
#include "linkedListType.h" template<class T> class orderedLinkedList: public linkedListType<T> { public: void mergeList(orderedLinkedList<T> &list1, orderedLinkedList<t> &list2) { first = list1.first; ... } private: };
There is more code in my mergeList() function, but I included the line that is giving me problems. The error that my CodeBlocks compiler is giving me is that 'first' was not declared in this scope.
Strangely enough, if I change it to this->first, the error disappears.
1. Why does it not recognise 'first'? 2. Why would this->first work at all? Is the 'this' object a smart pointer?
In my program, I create controls by deriving base objects of them I've made. These controls are then are attached within the OnCreate() function via a method I've created. For example:
Code: class tChat: public TextBox { public: void OnKeyDown(UINT &KeyCode) { if (KeyCode == VK_RETURN) { MessageBox(NULL, "Pressed enter!", NULL, 0); [Code] ....
The use of AddControl() feels quite redundant and is only their to parse a pointer to txtChat's Parent. I'm trying to see if it's possible to remove this line and automatically associate txtChat to fMain.
The user can then derive the Form and Controls and use their virtual OnEVENT functions to handle all the messages they expose.
So far my first concept is using the order-of-creation based on base-class constructor's being fired to determine which object is associated with what.
If I create a copy of a class (i.e. a Form-derived object), first the Form's constructor is fired, and then the constructor's of any class-based member-variables are fired. Is this a safe assumption? I imagine the only time this could be affected is by another thread creating the object of a Form or Control derivative?
If this assumption is true, I could save the 'this' pointer from the FormBase constructor, and then associate it with each Control via the base Control class' constructor? Then to ensure thread-safety, I could map the current FormBase pointer to the local thread id to ensure no conflict if multiply threads are creating forms at the same time?
I've created some mock-up code before trying to implement this into my main code. The following keeps track of the current Form being created by using a ThreadId-based map. When a control is created it gets the FormBase pointer based of it's ThreadId calling. The control then calls an Attach() function of it's parent Form using the pointer it just got, and parses a pointer to the control. The Form then adds the control's pointer to a list. When the Form eventually parses WM_CREATE, it automatically pulls the controls from the list and fires their virtual Create() functions to build them.
Mock-up:
Code: #include <Windows.h> #include <string> #include <map> #include <list> class FormBase;// Forward declaration class FormMap;// Forward declaration class Object {};// Base Object
[Code] ....
Is this plausible to use? I imagine C++ does not have many "guarantees" about how it creates objects and when. But I thought it would be safe that it would never create member-variables before the class of them is first created?
I am making a very basic parent/child class based program that shows polymorphism. It does not compile due to a few syntax errors reading "function call missing argument list. Lines 76 and 77, 81 and 82, and 86 and 87.
#include<iostream> using namespace std; class people { public: virtual void height(double h) = 0; virtual void weight(double w) = 0;
Let's say I have a Car object , and it contains inner Engine object.
Code: struct Car{ Engine mEngine; };
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code: Car::Car: mEngin(arg1,arg2,...) { other stuff here }
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code: class Toyota: Car(...), mEngine(...), mGear(..) { ... };
The other options are: 1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive 2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
# include <iostream> # include <cstring> #include <iomanip> #include <cmath> using namespace std; class Course // Creating the class Course
[Code] ....
Errors: Warning1warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
[Code] .....
I have to create an Array of type Course and then fill its member dats using various member functions. Those errors are caused by some Constructor defect, which I dont really know what it is.
It looks useful to implement strategy pattern because it makes a fully separate code block. So I can add a function to the map simply by compiling a source file. It's very simple. I don't need to edit another file.
But when I use it for my existing project, It makes some linking and runtime errors.(vs 2012). I can't recognize exactly why because it is a huge project. Anyway, I have a question that - Is this a safe use of class constructor?
I know that there is no fixed order of running, but in this case I think it doesn't matter. because they are independent. But it is not a common pattern, so I can't decide to use it.
The problem I have is with the Size constructor and the abstract class LevelObject which size is a member of.
The compiler error I get is:
C:Program Files (x86)ProgrammingProjectsUniversityprg_interactivesnakey_takeysrc..inc..incPlayer.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]| C:Program Files (x86)ProgrammingProjectsUniversityprg_interactivesnakey_takeysrc..inc..inc..incPlayer.hpp|17|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
[Code] .....
However I do invoke the copy constructor when I pass a variable of type size to the constructor in this line:
size_ = Size(s);
But the problem is that its complaining that the abstract class LevelObject doesn't invoke the constructor, which it shouldn't.
When the below is done, does it call the constroctor only, and if yes, constructors do not have return types so how does it work? is there anything behind the scene?
wxAddHandler(new wxPNG_HANDLER); and sf::RenderWindow(sf::VideoMode(...),...);
Can I determine if a templated class has a particular constructor, in my case using a string within function to which T is used?
Code:
template<class T> void MakeObject(std::vector<T>& dataVector) { std::string str "con string,Joe,24"; // catch if T has string constructor T someObject(str); // T someObject should have constructor from string dataVector.push_back(someObject); }
I am unable to understand how a move constructor works in this example of code. If someone could break down the process of what is taking place and explain to me on why to use a move constructor.
Code: class MyString { MyString(MyString&& MoveSource) { if( MoveSource.Buffer != NULL ) { Buffer = MoveSource.Buffer; // take ownership i.e. 'move' MoveSource.Buffer = NULL; // set the move source to NULL i.e. free it } } };
Example from "SamsTeachYourself: C++ in One Hour a Day"