C++ :: Pass Object To Class Which Stores It In Container As Unique Pointer

Jul 24, 2013

class A (abstract)
class B : A
class C {
void add ( A(&*?) a )
std::vector<std::unique_ptr<A>> data; //unique_ptr<A> because A is abstract and therefore vector<A> isn't possible
}

upper situation. What is the best way to pass add an object of class B to C?

with C::add(A* a){ vector.push_back( unique_ptr<A>(a) ); }
and
int main() {
C c;
c.add( new B() );
}

This works, but i don't think it's very nice, because you could delete the pointer in main. What happens then with the unique_ptr? I could probably make C::add( std::unique_ptr<A> u_p ); but maybe it can be avoided that the "user" (in main() ) has to create the unique_ptr itself.

View 10 Replies


ADVERTISEMENT

C++ :: Pass Object As A Pointer In Function

Apr 12, 2013

i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.

main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"

[Code].....

View 7 Replies View Related

C++ :: Pass Double Pointer Of Object Into A Node Of Self Similar Type?

Nov 30, 2014

The following code compiles and runs fine till it reaches line 16 and gets a sigsev violation which I'm not sure about as to why. I have no problem passing the object of type node** into the constructor of base and storing it into the double pointer node** copy;; but when I call the function void pass(node** temp) it crashes.

#include <iostream>
class base;
class node {
private:
node** data;
public:

[Code] .....

View 3 Replies View Related

C/C++ :: Container To Store Objects Under (unique) ID

Jun 14, 2014

I searching for a container/collection, that can access very fast to objects with the associated id.

Arrays are a bad idea, because it can be that I must store objects with a big id for example 9394034, so the array would be to big.

View 7 Replies View Related

C++ :: Pass Class Object By Reference

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

C++ :: Pass Class Object By Reference?

Mar 17, 2014

I'm trying to pass a class object by reference.

total = mathfunction(i);
}
double mathfunction(retirement& j)
{
double R = 0.00, m = 0.00, r = 0.00, t = 0.00, totaled = 0.00,
numerator = 0.00, denom = 0.00, temp = 0.00;

[Code]....

I only tried to pass by reference because I figured passing by value would be a larger pain in the neck.

View 1 Replies View Related

C :: Print Pascal Triangle And Stores It In A Pointer To A Pointer

Nov 27, 2013

I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.

Code:
#include <stdio.h>
#include <stdlib.h>
void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) {
FILE *fp;
fp=fopen("writePascalTriangle.txt", "w");

[Code] ....

View 4 Replies View Related

C++ :: Calling Defined Function Pointer From Another Pointer To Class Object?

Aug 19, 2014

I am attempting to implement function pointers and I am having a bit of a problem.

See the code example below; what I want to be able to do is call a function pointer from another pointer.

I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.

class MainObject;
class SecondaryObject;
class SecondaryObject {
public:

[Code]....

View 10 Replies View Related

C++ :: Pass By R-reference - Unique Objects That Have Been Explicitly Moved

Jan 19, 2012

I'm using mingw with gcc 4.4.1. I'm trying to write a function that operates only on unique objects that have been explicitly moved.

Code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<int> unique;
void eat(unique&& i)
{
std::cout << *i << std::endl;

[Code] ....

This is confusing to me because it compiles, yet my unique i is passed by R-value reference, without ever being explicitly moved. I thought named objects could NEVER be considered R-value references... Am I wrong to think that the above is extremely dangerous? After the call to "eat", my "i" has been destroyed internally, I could save by passing by value:

Code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<int> unique;
void eat(unique i) {
std::cout << *i << std::endl;
[Code] ....

But at this point, I'm paying for a (theoretically cheap) move copy, when I could be paying nothing at all... A third solution would be to overload with a const ref version, but at this point, I get an obscure linker error, when I'm really looking for a compiler error...

Code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<int> unique;
void eat(const unique&);
void eat(unique&& i)

[Code] ....

I'm actually trying to write a container that manages unique objects, and was trying to write a function that accepts only moved objects.

View 4 Replies View Related

C++ :: Pointer Class Object Vectors

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

C++ :: Destructor For Class That Holds Pointer To Object

Mar 20, 2013

Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). My question is : is it possible to overcome that without memory leaks ?

1 #include<iostream>
2 using namespace std;
3
4 class A {
5 public:
6 ~A() {
7 cout<< "~A()" <<endl;

[Code] ....

View 5 Replies View Related

C++ ::  check Pointer To Class Object For Null Value?

Feb 14, 2013

lets say I have a pointer p_unit of type c_unit* (c_unit is an a.b.c.)

I have a function that returns a pointer to a new c_unit object:

c_unit * man_add_unit() {
c_unit * local_p_unit;
unsigned short int local_run_code;
print_man_add_menu();
local_run_code = get_a_run_code(); // this bit just gets user input
switch (local_run_code)

[code]....

I assign that to p_unit, then add it to a vector v_units:

p_unit = man_add_unit();
v_units.push_back(p_unit);
cout << "New unit added.
";

The whole program runs on a loop, and another thing the user can do is to print out data on c_unit objects pointed to by v_units. The problem is, in that function up there ^ I give the user the option to go back to main menu without creating a unit.

Since "local_p_unit" is declared but not assigned an initial value, I'm guessing the function would return a "null" pointer (which is what's hanging me up). If I just let this run with the above code, and go to print out the unit data, the program crashes.

I tried to make an if thing with p_unit == 0 but this always returns false and doesn't catch the "bad" unit that will subsequently cause a crash.

Btw, I have considered assigning a reference to a generic c_unit object to that there local_p_unit so it won't return null, then remove pointers to that object from v_units at the end of the loop.. But I know there's got to be a better way.

View 4 Replies View Related

C++ :: Copy Derived Class Object Through A Pointer To Base

Jan 14, 2015

Is there a way to copy a derived class object thru a pointer to base?

For example:

class Base { public: Base( int x ) : x( x ) {}
private: int x; };
class Derived1 : public Base { public: Derived( int z, float f ) : Base( z ), f( f ) {}
private: float f;};
class Derived2 : public Base { public: Derived( int z, string f ) : Base( z ), f( f ) {}

[Code] ....

The question is whether *B[0] would be a Derived1 object and *B[1] a Derived2 object?If not, how could I copy a derived class thru a pointer to the base class?

View 1 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

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

C++ :: Deleting Object From A Container?

Nov 21, 2013

I have a container in c++11 and i must delete first object and i don't know how...

for (const prob& p : *static_cast<vector<prob>*>(list.listData)){
// i must delete the first object
}

View 2 Replies View Related

C++ :: Pass Matrix By Reference Using Pointer Of Pointer

Jun 20, 2014

i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):

#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)

[Code]...

View 6 Replies View Related

C/C++ :: Implementing Container Class That Holds Point Class

Nov 28, 2014

I have a Point class that's already implemented. My goal is to implement a container class called Line that holds the Point class. I'm not allowed to use any existing container classes for this. Here's what I'm working with:

//File: Point.h
#ifndef POINT_H_
#define POINT_H_
class Point {
public:
Point(int x = 0, int y = 0);
Point(const Point & t);
virtual ~Point() {};

[Code] ....

How I'm supposed to write Line.cpp...how do I access/add Points to Line without using something like a vector? I probably should've included what I've written so far.

#include "Line.h"
/**
* Default Line constructor
*/
Line::Line() {

[Code] ....

View 6 Replies View Related

C++ :: Function To Use Unique For Class?

Dec 6, 2013

if i can use a function to use unique for my class and also i want to count how much each of them is duplicated. i mean i have (420,250,420,66,444,777,250) in my list i would like to know that 420 is duplicated 2 times and also 250. Is there a way to get this result ?

list<Patient *> ListePatient
ListePatient.unique(g_nas()); // nas is a attribute of the class patient

View 2 Replies View Related

C++ :: Trying To Pass Array Of Object

Apr 24, 2013

I am trying to pass an array of objects however i m finding some problems.

pieces.h

Code: class Cpiece{
public:
Cpiece* board[8][8];// array of objects
virtual char getcolor() =0;
virtual char setcolor(char colour)=0;

[Code] .....

View 10 Replies View Related

C/C++ :: Pass Value From Object To Array

Apr 16, 2014

I am trying to write a poker program. I created a class to create a deck of cards..and then shuffle the deck..Now I am trying to pass value from the deck to an array to deal a hand..so 5 cards to player1[] array.. I tried doing it directly such as.

for (int j = 0; j < 5; j++) {
getCard=deck[j].toString();
}

But I would get error such as [Error] no match for 'operator=' in 'player1[j] = deck[j].Card::toString()'

So then I tried using a pointer..

char *getCard;
getCard = new char;
for (int j = 0; j < 5; j++) {
getCard=deck[j].toString();
}

but I would get this error [Error] void value not ignored as it ought to be. So how can I pass a value from the object deck..to an array? or should I be doing it another way?..I tried to think of a way to do it via a function but really got hung up there..

here is full code

#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const char* FaceString[13] = {"Ace", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};

[Code] ....

View 11 Replies View Related

C++ :: How To Pass Same Instance Of Object In Two Functions

Feb 25, 2014

I need to send same instance of object of a class in two function (Main function and thread function). The class is something like this:

//The class need to have constructor.
Class ABC {
public:
DWORD *IdG;
ABC(int number) {
IdG = new DWORD[number];
}
}obj(32);

The obj(32) is called in following two function. Function F1 is called using thread in main function.

void F1() {
obj.test;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
obj.test1;
_beginthread(F1,0,(void*)number);
}

The code works well when the object of class ABC is created as shown above. My problem is the value that is passed in the object ('32') is to be read from the file.

If I read the file and create object separately in Main function and function 'F1' then , function 'F1' is not executed.

How to create same instance of object for Main function and function 'F1' with value passed in the object taken from the file.

View 1 Replies View Related

C++ :: What Is Container Class

Sep 22, 2013

What is container class? what its advantage ?

View 2 Replies View Related

C# :: Determinate Type Of Object And Pass It Through Method

Sep 18, 2014

I got base class called Statistic and inherited classes from it : lsp, cpu, memory. Those methods inheriting some of methods from Statistic and implement also theirself methods. I prepared some new class called ExecuteRequest which is taking Statistic object in the constructor. What i want to achieve is after i put e.g LSP to this class i would like to determinate what specific object it is in this case LSP right? Then having that i would like to put this object to RunRequest method. How can i achieve that? See my code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SOAP_testing {
public class ExecuteRequest

[Code] .....

View 14 Replies View Related

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 View Related

C++ :: How To Create A Container Class

Feb 9, 2013

I know that it's a class that contains data from another class I just do not know how to make them both work together.

View 1 Replies View Related

C++ :: Best Std Container Class For Application?

Jan 16, 2012

I have a class called CTestObject and it has one variable int id. I want to add instances of this to an std container and be able to:

1. quickly determine if the value of "int id" exists in the container and update the object

or

2. add a new object to the container

Which std container is the best for this application? What method should I use to search the container for the object id?

View 14 Replies View Related







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