C/C++ :: Objects Hold References To Other Objects?

Nov 12, 2014

This has been bothering me for a while now, and I finally put together an example:

#include <iostream>
#include <string>
using namespace::std;

[Code]....

In the code above, the two classes hold pointers to each other, and that's fine but it doesn't seem right since C++ prefers to pass by reference. Yes, it can still do that (see testbox and testball) but even that seems odd to me because still you need to use pointer notation for the enclosed object. Am I the only one who feels this way, and should I just get over it? Or am I missing something that would allow an object to hold a reference?

View 4 Replies


ADVERTISEMENT

C++ :: What Are Rvalue References And Temporary Objects

Dec 30, 2014

What rvalue references are? How are they useful? What are temporary objects?

View 1 Replies View Related

C++ :: Testing For References To Identical Objects

Jun 18, 2014

I am checking to see if two references are bound to the same object. My instincts tell Me, "Check their addresses. If they match, they are bound to the same." At the same time, I have not found anything in the C++ standard which would support this approach. Am I missing something? Is there wording which backs up My instincts? Is there a standard function to do this?

View 4 Replies View Related

C++ :: Hold Vector Of Pointers In Same Class As Instances Of Those Objects?

Feb 10, 2015

Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this

class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level

[Code]....

When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?

View 2 Replies View Related

C++ :: Constructing Text Adventure - World Class To Hold Objects From Character

Jun 19, 2014

I am working with a new text adventure. The way i want to construct it is by having a class for all living things. in the class you have basic things as: health, gold, vector for inventory holding "struct item". etc...

There is also a class called world, wich navigates through the world.

World class contains of: player location, and a map containing info about the room etc...

Here comes the problem. I want there to be characters to be placed out in different maps, so basically i want the world class to hold objects from Character.

How to do it. In world class i made a map...

std::map<int,"content">

content is a struct i made above in world class:

struct content{
std::string name; // location name
std::string info; // info about location
std::vector<Character>characters;
std::vector<item>items;
};

To sum it up, i have a std::map<int,content>map the int stands for location id. Content holds more info about the room and what's in it

btw the classes are in different files and that means i have to include "Character.h" in the world file so i can set up the vector of characters.

View 2 Replies View Related

C++ :: How To Hold Pointers / References To Abstract Class

Nov 15, 2014

I have an abstract class named Terrain, and a class named RoadMap, which supposed to hold an N*N array of Terrains. But I'm not sure what type should the RoadMap class hold:

Code:
#ifndef TERRAIN_H
#define TERRAIN_H
class Terrain {

[Code] ....

I can't use an array of refernces here, so I tried this:

Code: Terrain** terrain; and then I thought this was the way to go:

Code: Terrain (*terrain)[]; But now I'm not sure.

The N*N matrix size supposed to be determined according to a given input... What type should I use there?

View 2 Replies View Related

C++ ::  attraction Between Objects

Apr 2, 2013

i am relatively new to c++, and am trying to make it so particles will gravitationally attract to the mouse pointer. i have gotten all the bugs that the compiler found, but there must be another because the program crashes the second i open it. my code could be way off, so just tell me if it is and ill do more research and learning before trying something like this again. here is the code:

#include <SDL/SDL.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
using namespace std;
float mouseX;
float mouseY;

[Code] .....

View 6 Replies View Related

C++ :: The Proper Way To Do A Vector Of Objects

Oct 20, 2014

I've been really busy but managed to get in enough down time to learn somewhat decent info about vectors. Anyways originally my program created a dynamic array of pointers to class objects and I came across a few problems because of this. Apparently an array of pointers is now outta of the question and I will now be switching to a vector of objects instead.

Why I want a list of objects instead of pointers this little comment should clear things up.

tiles[i]->show() dereferences tiles[i] (i.e. accesses whatever it points at) before calling the show() method.

That is undefined behaviour. Once undefined behaviour occurs, there is no recovery, and there is nothing the show() method (or any subsequently called function for that matter) can do to recover (short of invoking their own forms of undefined behaviour - compiler specific hacks, etc).

Even if the show() method initialises the object correctly, it cannot change the pointer tiles[i] which is in a different scope (within main()).

What I'm trying to do is create a vector of already intialized objects so that I can use a conditional statement of every single element to properly layer my games art resources. This should also automatically fix a mild unrelated collision dectection problem too but first thing first layering.

View 9 Replies View Related

C++ :: How To Delete The Objects Within A Vector

Nov 10, 2013

here's the problem. I want to delete the objects within a vector, although I'm not sure whether I should clear the vector afterwards. Is it really necessary?

Code:

for (i = 0; i < allSales.size(); i++)
delete allSales[i];

allSales.clear(); //Is this step necessary?

View 5 Replies View Related

C :: How To Compile Objects In A Subdirectory

Aug 25, 2013

well the question is how to compile objects in a subdirectory?

Code:

./bo/%.o : %.c
$(CC) $(CFLAGS) <$

the command abouve will compile my objects but leave them in the same directory. However I want them in a different directory. in ./bo directory. How to achieve that ? I just realize that if i remove ./bo/ from the line it does not work..

View 3 Replies View Related

C++ :: Assignment Of Objects To Pointers

Jun 6, 2013

I am using OpenCV to read and manipulate a set of images, which I need to store in an array to work with them. Here is a snippet of the code:


#define MAX_IMAGES 8
typedef Mat* MatPtr;
int main(int argc, char** argv) {
char imageName[] = "./res/imageX.tiff";
MatPtr datacube[MAX_IMAGES];

[code].....

I have an array of pointers to Mat objects (an OpenCV class used to hold info and data about an image), which I will use to store the images. The function imread reads an image and returns a Mat object loaded with the relevant data about the image.However, this gives me a nice segfault when the assignment takes place. Of course, I can swap it with the following code, but since I'm working with big images (2048x2048 and upwards), it's really inefficient:

for(unsigned int i = 0; i < MAX_IMAGES; i++) {
imageName[11] = 49 + i;
datacube[i] = new Mat(imread(imageName, -1));
}

Is there any way to do this elegantly and without much hassle?Again, excuse my rustiness and anything completely stupid I might have said. It's been a long time since I worked with C++. Managed to circumvent the problem by using a STD vector instead of an array. I'd still like to know the answer to this riddle...

View 6 Replies View Related

C++ :: How To Use Class With Array Of Objects

Oct 12, 2014

class FlashCard {
public:
int a,b;
void PrintCard(void);
int CorrectAnswer(void);
};
void SwapFlashCard(FlashCard &a,FlashCard &b)

[Code]....

Why does Xode warn me that Type'FlashCard'does not provide a subscript operator on line 22,23,29 and 30?

View 7 Replies View Related

C++ :: Access Objects From Another Thread

Jun 25, 2013

I have the following code below. I am getting a memory access violation when accessing cmd->query_string in the loop function. The loop() function is running in another thread. The cmd object appears to be destroyed after calling the send_command function. How do I create an object on the heap and access the query_string.

std::list<my_command_message_que*> my_command_que;
void loop(){
if(my_command_que.size() > 0){
my_command_message_que * cmd = my_command_que.front();
std::cout << cmd->query_string;

[Code] ....

View 2 Replies View Related

C++ :: Bad Value Result From Operator Using Objects

Jul 24, 2013

I keep getting an undesired value in this code. I've tried several methods, but none are giving me the correct answer. The out put is always zero, when in this case it should be 10!!

Here's the object structure:

template<class T, class _b>
struct quantity {
private: T value;
public:
explicit quantity(T val): value(val) {};
T getValue() { return value; };

[Code] .....

Here's the operation:

int main() {
quantity<int, bool> m(20);
quantity<float, bool> m_f(10);
quantity<double, bool> m_d(NULL);

m_d = m_f;

[Code] .....

Why does it seem that the assignment operator is the harder operator to overload? Maybe it's just my luck, but I seem to always run into issues whenever I work with it. I hardly ever experience errors when overloading any of the other operators.

View 6 Replies View Related

C++ :: Headers And Objects Files

May 20, 2013

I have a question regarding how GCC relates a header file and its binary file.

main.c
#include <math.h>
#include <stdio.h>
int main (void){
double x = sqrt (2.0);
printf ("The square root of 2.0 is %f
", x);
return 0;
}

we can compile it by running the line: gcc main.c -lm -o main

My question is: How GCC knows where is the definition of sqrt?

First I was thinking that there was and object file with the name math.o inside libm.a (that is GCC will look for an object file with the same name as the header file), but after running the next line I think my assumption was wrong as there is not such file in libm.a.

nm libm.a | grep math.o
nm: e_acos.o: no symbols
nm: k_cos.o: no symbols
nm: k_sin.o: no symbols

[Code] ....

View 7 Replies View Related

C++ :: How To Store Objects To Program

Nov 14, 2013

I've started programming my little program called vLibrary (program I want to make for the library in my city) and after I m done with console application I will try to implement wxWidgets.My program will be able to add new users to the system, new books and new librarians.

Now, I m confused what data types to use and how to store objects (newly created users, books etc) to my program so later on they can log in the system etc. Logic of the program is completely clear to me but I m not sure how to make array of objects and store them in memory or in a certain file, how to store password and make some kind of encryption etc.which data structure from STL should I use and how.

View 2 Replies View Related

C++ :: Accessing Objects Within Another Object

Feb 8, 2014

I'm currently trying to access a variable contained within a base object from another completely different object and I continually get error messages. I'll attempt to put up the important code since it's contained within fairly large classes. From the Base .cpp file. ObjectPosition:

void ObjectPosition::init(float x,float y, float speed, int boundx, int boundy, float hit, int lives, bool live) {
ObjectPosition::x = x;
ObjectPosition::y = y;
ObjectPosition::speed = speed;
ObjectPosition::boundx = boundx;
ObjectPosition::boundy = boundy;
ObjectPosition::hit = hit;
ObjectPosition::lives = lives;
ObjectPosition::live = live;
}

This is the initialization function for the BaseObject. All objects are inheriting these variables which are Protected within the ObjectPosition class.Then they are initialized within the Pig class thus wise:

void Pig::binit(float sx,float sy, ALLEGRO_BITMAP *simage) {
//Sets all ObjectPosition Variables
ObjectPosition::init(800,900,10,80,40,40,10,true);
smaxFrame = 4;
scurFrame = 0;

[code]...

I tried to initialize the boundx through the pig via pinitx but I get errors and I can't access through the pig to the object position to the boundx.

View 10 Replies View Related

C++ :: Looping Through Template Objects In Map

Dec 30, 2014

How to get this code working

template <typename T>
class Dummy {
// Implementation
};
template <typename T>
class SomeClass

[Code] ......

View 4 Replies View Related

C++ :: Storing Objects Using Vector

Oct 31, 2014

I have two classes, Parent and Child, where Parent contains a vector that is used to store instances of Child. When I create an instance of Parent, three instances of Child are also created in Parent's constructor and stored in the vector. I understand that push_back() creates a shallow copy of each Child instance and that Child's destructor is called each time the loop (inside Parent's constructor) iterates. The problem is that because Child's destructor is called each time the local variable child goes out of scope, the memory previously allocated in Child's constructor is destroyed and when Child's destructor is called again later on in the program to get rid of the copy stored in vector, the program crashes. I can fix this by overriding the default copy function or by storing pointers to objects instead of copies of objects. I don't really need to use vectors in this case since I always have three children in one parent but I'm doing this as a learning exercise and would prefer to use vectors.

#include <iostream>
#include <vector>
class Child {
public:
Child() {
std::cout << "child constructor called" << std::endl;

[Code] .....

View 3 Replies View Related

C++ :: Deposit Vectors With Different Objects?

Nov 6, 2013

I my code i have a base struct ...

Then others structs made ...

struct base{};
struct a1 : base {
int a;
int b;

[Code] ....

Now i must deposit all these containers(maybe & of containers) in another vector...

How i can do that? Can i write

vector<vector<base*> > all;
all.push_back(&v1);
all.push_back(&v2);

View 14 Replies View Related

C++ :: Using Array Of Pointers To Objects?

Sep 27, 2013

I've created an Array of pointers to objects using:

Person ** A = new person * [arraysize];

When I intend to access a specific person do I have to do this? :

something = A->[i];

and when I want a specific object within my struct do I have to do this? :

something_else = A->[i]->random_int;

View 10 Replies View Related

C++ :: Store Address Of Objects

Oct 27, 2014

I'm using the SDL library and trying to match the C++11 standards... Anyway, I thought about a vector where I store all the addresses of game instances, so I can access them anytime... I tried with this function:

int instance_new(std::vector<uintptr_t> &instance_id, unsigned &instance_size, Instance *pointer) {
instance_id[instance_size] = reinterpret_cast<std::uintptr_t>(pointer);
instance_size++;
instance_id.resize(instance_size);
return 0;
}

Where "Instance" is the 'parent' class of various child classes like the player. So, if I have to search the existing of a thing in my game, I should check if the address references to an instance of class. How can I do this?

View 1 Replies View Related

C++ :: Objects Interaction With Functions?

Mar 1, 2014

I am preparing for an exam, and I am being told there will be, "1 multiple-choice question about reading a piece of code about objects interacting with functions and deciding the output."

There are a few ways I can think of:

- Passing an object into a function as an argument
- An object calling a function with a dot operator
- Creating a new object through a Constructor and Class(Does that count?)

Are there other ways that objects interact with functions that I am not thinking of?

View 1 Replies View Related

C++ :: Container Of Hierarchy Of Objects

Apr 25, 2014

How do we design a container of objects all of which belong to some subclass which directly/indirectly inherits from a given parent class? Moreover, I would like to have functions that enable me to pick only objects of a certain class type from the container.

For example if the parent class is A and I have a hierarchy of classes that derive from it, we must have a container that can contain any class that exists in this hierarchy. Also, get_B() must be able to let me examine only those objects in this container that inherit (directly/indirectly) from class B (class B exists in the hierarchy rooted at A).

Preferably, we would like to avoid downcasting. Or even explicit typechecking of any sort.

View 8 Replies View Related

C++ :: Add Three Objects Of A Class Matrix

Apr 9, 2014

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.

Matrix obj1, obj2, obj3, obj4
Obj1=obj2+obj3+obj4

I am using the following code for this

Matrix Matrix::operator + (Matrix &obj) {
if((this->Columns == obj.Columns) && (this->Rows == obj.Rows)) {
for(int i=0;i<obj.Rows;i++)

[Code] .....

the problem is that it is just adding only 2 operands. How can I add more?

View 2 Replies View Related

C++ :: How To Set Up Two Objects To Communicate With Each Other Asynchronously

Jul 2, 2014

Here is what I've got so far:

// build:
// clang++ -g -std=c++11 -Wall main.cpp -o main
// main.cpp
#include <iostream>
#include <thread>
#include <vector>
void pt_ssleep(const int delay);

[Code]...

I need oes object to send some data back to oms object asynchronously and to insert those data into numbers container.

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved