C++ :: Automatically Attaching Objects To Parent Class
Jul 8, 2013
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 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?
template<class T> class Singleton; class Base; class Sub : public Base, public Singleton<Sub>;
I' using underlying auto pointers, that's why Singleton is a template class and Sub passes itself as a template parameter. I'm developing Singleton and Base and a public API allows anyone to add their own sub classes. I actually want a real triple hierarchy like this:
template<class T> class Singleton; class Base : public Singleton<Base>; class Sub : public Base;
So that external developers don't have to worry about templates and complexity. The problem with this is that my implementation in Singleton will now call the constructor of Base whenever I create an instance of Sub (since the template parameter is Base).I was wondering if this could be done by pre-processor macros:
template<class T> class Singleton; class Base : public Singleton<__CLASS_NAME__>; class Sub : public Base;
Where __CLASS_NAME__ is the class name that will be replaced by the pre-processor. Theoretically this should be possible, since the __PRETTY_ FUNCTION__ macro actually returns the class name. The problem is that one cannot do string-manipulation to remove the function name from __PRETTY_FUNCTION__.
how I can accomplish this so that the Sub class is not aware of inheriting from a Singleton<template> class?
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?
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;
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
#include <iostream> using namespace std; class superclass; class subclass1; class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
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 .
I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.
I need an array of class objects but am unsure of how one might accomplish this. I have so far...
//element class driver code Element Arsenic(lowCeiling, highCeiling); Element Cadmium(lowCeiling, highCeiling); Element Chromium(lowCeiling, highCeiling); Element Copper(lowCeiling, highCeiling); Element Lead(lowCeiling, highCeiling); Element Nickel(lowCeiling, highCeiling); Element Molybdenum(lowCeiling, highCeiling); Element Mercury(lowCeiling, highCeiling); Element Selenium(lowCeiling, highCeiling); Element Zinc(lowCeiling, highCeiling);
What I want to do is have an admin class which will hold all the employee objects, can add them, list and calculate salaries. I'm trying to make array of objects, not sure if it's right
here is the code
Code: #include <iostream> #include <string> using namespace std;
class Employee { public: Employee(string name, short type, int salary)
For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?
Here's some snippets of the relevant code.
class Employee { .... virtual void printPay() = 0; }; class HourlyEmployee : public Employee {
i ran the following code in the latest version of code::blocks and it tells me that the objects cout and cin are not declared in this scope. what is the problem?
I used to use Turbo C++ 3.0 and i had no problem whatsoever with that compiler. But now i am trying to move to code::blocks but it is proving very very hard as all the standards have been changed.
I am a school student and thus, we had been told to practice on Turbo C++ 3.0 and now i am unable to unlearn it. Also, if i use printf in place of cout there is no error but i want to use cout as it is what i am comfortable working with.
#include<fstream> #include<conio.h> int main() { using namespace std; char name[20];
[Code] ....
Is there some document to which i can refer so as to get the latest C++ standards which is C++0x i believe?
I am working on a project that requires me to create objects from a abstract class that has 2 child classes (that need to be derived). Any examples on how to do this? I looked online and the examples were pretty vague. the main error that I am getting is when I make a temp object with & in front of it (such as Employee &genericEmp) it throws a must be initialized error.
Running into a snag in my program. I can't seem to figure out how to have an object of a class be able to look at all the other objects of its own class.
Reasoning being, I'm working on a game with multiple ships flying around in the same space. Each ship is a class. Each ship has an x and a y, and needs to compare the angle and distance of other ships' x and y coordinates to see if they're visible on the same screen.
How to tell an object to look at objects of its own class.
Here's some code:
common.h
#ifndef COMMON_H_INCLUDED #define COMMON_H_INCLUDED int dist(int x1, int y1, int x2, int y2); int get_info(int which);
[Code] ....
Basically, I just don't know how to properly write a function, call, or how to get the info I need, to the Player::get_closest() function so that it can see the other play objects.