C++ :: How To Get Object Of Original Class From Function Of Other Class Where Other Class Object Is Member Of Original Class
Jan 21, 2013
The case is like
class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}
in somefunction i want a pointer to current object of class A m new to c++
View 2 Replies
ADVERTISEMENT
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related
Jul 3, 2014
I have a method to take a Tile object and make an instances of it based on some data from the original object. Than it is suppose to manipulate the a specific instance and save the results. The first loop through it works but it changes all instance as well as the base.
public static int recurse(int count, Tile[,] b,Huristic h,int check) {
if (check==1) {
boardState.Add(B)/>;
return check;
} if (check == 0)
[Code] .....
View 6 Replies
View Related
Apr 26, 2014
I have my main.cpp like this:
#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}
Can I use a function to simply put values from old to new variables? If yes then how?
View 3 Replies
View Related
Aug 20, 2013
What is the size of object in c++ , if there is no data member in the class ?
View 3 Replies
View Related
Sep 20, 2013
Suppose I have two classes A and B so how to access object of class A in constructor of B's class as a parameter.
View 6 Replies
View Related
Feb 20, 2015
My thought is that I would need to establish a variable for the class in the header and call the class within the .cpp file
//class A.h
B b;
Then in class A, to create the object and use a method from the object I would -
//class A.cpp
A::A(){
b();
}
int someAmethod(){
b.bmethod();
}
View 6 Replies
View Related
Mar 7, 2014
I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock. Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock {
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)// more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)
[code]....
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
View 5 Replies
View Related
May 27, 2014
i want to store reference to a const object in my class as a member variable, as follow:
I basically want a readonly reference to |Data| in Device object.
Code:
class Device {
Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {
} protected:
const QList<QSharedPointer<Data>> & _listRef;
}
This does not allow me to initialize _listRef as something like NULL when it is not applicable.Also, i must change all my constructors and its child class to include an initialization of _listRef!!
What is the alternative? Is pointer the nearest? which of the following should be used?
Code:
const QList<QSharedPointer<Data>> * _listRef;
or
const QList<QSharedPointer<Data>> *const _listRef;
or
const QSharedPointer<QList<QSharedPointer<Data>>> _listRef; ????
View 7 Replies
View Related
Jul 14, 2014
Information:
I'm using Code::Blocks v12.11.
(I'm using C++/SDL2, but I think that's of no relevance)
Problem:
I create a class named "CSprite" in a "Sprite.hpp"-file. I create a "Sprite.cpp"-file, which includes the "Sprite.hpp"-file. I define the methods of the class "CSprite" in the "Sprite.cpp"-file.
When I try to create an object of "CSprite" in the class named "CPlayer" in the file "Player.hpp" I get an error message. (<-- Looks complicated I know, the code example will be more usefull than this)
Error in the build messages:
C:UsersLinoDocuments1 Data LinoFreizeit1 ProgrammierenC++ & SDL2The Running ManCPlayer.h|30|error: 'CSprite' does not name a type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|
Code Example:
Sprite.hpp
#ifndef _SPRITE_HPP_
#define _SPRITE_HPP_
class CSprite {
[Code] .....
What did I miss? Did I include the wrong file? Or did I Forget to include the file? Why do I get the error message?
I also tried it with a pointer declaration and the "->" Operator but I got the same error message. I know I could just write a new function to load the texture in my "CPlayer"-class but this would not really answer my question.
View 15 Replies
View Related
Nov 9, 2013
I fully understand that a variable of one simple data type cannot be assigned to another of a different data type in an assignment statement, except when type-conversion(i.e. casting) is employed. For structured data types (like classes), I know that the assignment operator can be overloaded; but this only addresses objects of the same type.
My problem is that I have read data from an input file into an object of one class, but I want to transfer the contents into an object of a different class in an assignment statement. Can this be realized given that the two operands of the assignment operator (=), in this case, would be of two different types?
View 5 Replies
View Related
Sep 18, 2013
How can a member function in my derived class call the same function from its base class?
View 1 Replies
View Related
Dec 13, 2012
#include "B.h"
class A {
public :
A()
{
s_b = new B();
b = new B();
[Code] ....
In my project i have seen static object as above . But not able to know what is the exact use of it and how they are different from general object .
View 2 Replies
View Related
Feb 23, 2014
I'm trying to write a function for receiving messages, so my classes can communicate with each other. The only issue I get is a compile error asking me to define the base parameter as one of the derived instances. I tried using a void* to fill the need, but then I lose the initial type, which I need to check for. How might I go about writing a generic object for this?
Here's my code:
template<class Object>
class State
{
public:
[Code].....
Should I just have all of the objects inherit in the order of Object >> GenericObject >> DerivedObject?
View 10 Replies
View Related
Oct 7, 2014
How to initialize a static member of a class with template, which type is related to a nested class?
This code works (without nested class):
#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>
[Code] ,....
View 1 Replies
View Related
Aug 12, 2013
I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code
I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class
[Code] ...
Here is the lines of code i want to understand.
Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);
I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.
View 3 Replies
View Related
Sep 4, 2014
So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?
View 19 Replies
View Related
Dec 21, 2013
I have two classes one called Date and the other is University , Date class has two overloaded operators , ostream and istream to take in the data and print them out :
ostream & operator<<(ostream & out, Date & x) {
out<< x.month << "/" << x.day << "/" << x.year ;
return out;
[Code] ....
The University class has an object of type Date called establishDate and I must use this to print out the date along with the University name and location, here's University class :
University.h
class University {
public:
University (); // constructor
friend ostream & operator<<(ostream & out, University & x); // print the university data
friend istream & operator>>(istream & in, University & x); // to read university data
[Code] .....
so how do I use the establishDate object ?
View 2 Replies
View Related
Jun 22, 2014
Sandy is of class Person, who converts to Muslim and takes on a new name Fatima, and has all the attributes of Sandy, with new Muslim attributes (e.g. religion == Islam, etc..). At this point, Sandy can be deleted, and Fatima, now of class Muslim will play Sandy's role henceforth. The problem is that due to her new address, all the people who knew Sandy does not know Fatima. Manually changing Sandy's address to Fatima's address for all those people who knew Sandy is clearly not an acceptable method. I looked through all the design patterns and cannot find one to solve this problem. how to improve the design? Here's my simplified code showing the problem:
#include <iostream>
#include <string>
#include <typeinfo>
class Person {
std::string name;
Person* bestFriend;
[Code]...
Output:
sandy = 0x32658, 6Person
Mary's best friend is Sandy.
fatima = 0x23fec0, 6Muslim
Mary's best friend is Sandy.
Of course, we want to have: Mary's best friend is Fatima,. with mary->getBestFriend()->getReligion() == Islam, etc... How to redesign the whole thing so that this is automated (assume there are thousands of people who know her)?
View 14 Replies
View Related
Jul 15, 2013
is it possible to don't declare an object for a class and use the function of this class ? i saw a code use this but i cannot find it again .
View 7 Replies
View Related
Jul 27, 2013
What is the purpose of having static object of the same class.
E.g.
class someObj{
public:
static someObj obj;
};
how the compiler treats this object
View 3 Replies
View Related
May 10, 2014
class abc {
public:
int i;
abc * foooo;
};
How do you call * foooo? Say I create:
abc a;
* foooo would have some values for int i.
To get int i of *foooo, I tried a.foooo.i, which doesn't work. How do you call it?
View 5 Replies
View Related
Jun 25, 2013
I am having trouble working with third party dll's, libs and header files. I am trying to call a function.here is the function that is suppose to be called.
bool COAuthSDK::GetRequestToken(CClientDetails &objClientDetails)
it has this info of what it needs :
Name IN/OUT Description
m_environment IN Optional. Possible values are SANDBOX (default) and LIVE.
m_strConsumerKey IN OAuth consumer key provided by E*TRADE
m_strConsumerSecret IN OAuth consumer secret provided by E*TRADE
m_strToken OUT Returned by the function if successful
m_strTokenSecret OUT Returned by the function if successful
m_strCallback IN Optional; default value is "oob"
here is the COAuthSDK header
#ifndef _OAUTHSDK_H_INCLUDED_
#define _OAUTHSDK_H_INCLUDED_
#include "ETCOMMONCommonDefs.h"
#include "ETCOMMONOAuthHelper.h"
using namespace std;
[code].....
when I try to build the function it says that its in the dll's so I know I have to call it.here is the link to the build site if needed URL....
View 1 Replies
View Related
Feb 5, 2014
hiclass Parent {
};
class Child : virtual public Parent {
};
What is the size of object of Class Child in following case?
View 17 Replies
View Related
Dec 24, 2014
I wanted to create a new class object and I want to name it from a string. Something like this:
string name = "Mike";
Customers name;
//to create a new object in customer class using the name string
Is there any way to do this?
View 11 Replies
View Related
Aug 4, 2013
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
struct bop {
string realname; //real name
[Code] ....
Okay, so first thing's first. The program will not compile due to lines 39-45. If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?
I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.
View 6 Replies
View Related