C++ :: Convert Pointer To Plain Object

Jul 23, 2013

I have pointer object and i have to assign to another variable ( new object ). But if i change any value in new object should not reflect in old object. I did in the below way, it changes the value.

class Test {
public:
int num;
};
Test* obj;
obj.num=1;
Test obj_n=*obj;
obj_n.num=2;

Now both object's num have value as 2;
i.e
obj.num // has 2
obj_n.num // has 2

Actually my expected result is:
obj.num - should have value 1
obj_n.num - should have value 2

My actual scenario is different where Test obj is pointer object, so obj should be pointer object. i have given sample scenario.

View 5 Replies


ADVERTISEMENT

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++ :: How To Convert Void Pointer To Int / Double / String Pointer

Mar 7, 2013

I have a function:

const void insertStuff(const void *key, const int value){
// I want to convert the void pointer into one
// of three types of pointers(int, string, or double)
switch(value){
case 0:
int *intPtr = key;

[Code] .....

But this causes an error of: "crosses initialization of int*intPtr"

What's the correct way of implementing this?

View 1 Replies View Related

C :: How To Shuffle Bits Of Plain Text According To DES Standard

Jul 14, 2014

I learned the bitwise operations in c, but how to shuffle the bits of the plain text accoring to DES's standard. Even with the key and the S-boxes. How DES is performed in C step by step. I have a fair knowledge in C.

View 3 Replies View Related

C++ :: Two Dimensional Pointer To Object?

May 24, 2012

I am migrating a scientific code from Java to C++.

What's wrong with the two functions? I can use the int** like a two dimensional array but not the Agent**.

I receive this error: "No operator = matches this operand".

(I have allocated two dimensional arrays for objectSpace and agentSpace somewhere else).

Code:
int** objectSpace;
Agent** agentSpace;
void Space::removeAgentAt(Point p) {
agentSpace[p.x][p.y] = NULL;
}
void Space::putAgentTo(Agent agent, Point newP) {
agentSpace[newP.x][newP.y] = agent;
}

View 8 Replies View Related

C/C++ :: Error - Cannot Convert To A Pointer Type

Apr 3, 2015

I have attached the source code I am writing. I have been getting the two following errors. I know it's something to do with the pointer casting.

udp-server.c:174:3: error: subscripted value is neither array nor pointer nor vector
udp-server.c:165:3: error: cannot convert to a pointer type

The file is attached here.
udp-server-edit.txt (7.64K)
Number of downloads: 19

View 3 Replies View Related

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++ :: 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++ :: Assigning Address To Object Pointer

Aug 16, 2013

I have the following code.

StackElement *StackElementArray;
StackElementArray = new StackElement[iMaximumDepth];

I want to assign one element of this array StackElementArray the address of another object. For example,

voidStackMem::push(StackElement &iStackElement) {
CurrentDepth++;
StackElementArray[0] = iStackElement;
}

The StackElement class contains pointers to some dynamic arrays. When I use the assignment, StackElementArray[0] = iStackElement;, it doesn't copy the complete contents and I have to define an 'assignment operator' function to copy all the contents. I am wondering if there is a way I can assign StackElementArray[0] the pointer to the StackElement object. In that case, I will not need to copy the contents of iStackElement into StackElementArray[0] and will just copy the address.

View 3 Replies View Related

C++ :: Function Pointer With Object As Parameter?

May 30, 2013

I'm making a code that uses a Function pointer. The problem is, when I try to compile appears an error like:

error: no matching function for call to 'rnVector::rnVector()'

Here's part of the code:

phiFunction::phiFunction(double (*f)(rnVector), rnVector (*df)(rnVector)) {
//... Here comes the code stuff...
}

View 12 Replies View Related

C++ :: Passing Object By Pointer To Function?

May 20, 2014

I have a Qt classes as follow:

Code:

class Vehicle {
public:
void AddData(QString str, Data* data) {
_myDataMap.insert(str,data);
} virtual void Init();

[code].....

My questions are:

After the main function called d1->Modify; the data stored in _myDataMap will get modified too.

What is the more appropriate way of passing the Data through AddData in such case?

If i do AddData(const Data & data), i will not be able to use inheritance of Data, i.e passing a subclass of Data to AddData.

View 3 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++ :: Constructor That Initializes A Pointer To Point To The Object

Aug 27, 2013

I have a program that has a base class 'control' and there are 2 dervied classes 'button' and 'textbox'. How do i make a constructor in the 'button' or 'textbox' that initializes a pointer of the data type 'control' to point to the object that invokes the constructor. the code should look like this

class control {
//data
} class button:public control {
buton() {
//code for the constructor
}
}

actually i have an array of pointers of the type 'control' and as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance

View 7 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++ :: Calling Member Function By Object Pointer

Nov 26, 2013

I have the following piece of code.

Code:
#include<iostream>
using namespace std;
class Test {
public:
Test(){cout<<"Test"<<endl;}
void fun() {
int i=5;

[Code] ...

Compiled with g++.

Executing this give output fun5.

It is correct? I have not allocated any object and so this pointer is not created. Then how it is able to run and call the function.

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++ :: Variable Declaration (const Pointer To SensorBase Object)?

Feb 19, 2015

I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration

Code:
SensorBase* const sensor(mSensors[i]);

It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??

View 6 Replies View Related

Visual C++ :: Convert BITMAPINFO Header Into Unsigned Char Pointer?

Jan 10, 2014

I have a bitmap header information in the struct

typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO

The total size of this is 1080.

Now i want to convert this into a unsigned char pointer.

unsigned char * pBMPHeaderData;

I already got the raw image data in another unsigned char buffer.

unsigned char* pRawBMPData;

Now i want to make a complete BMP image by adding the header info and raw data into a new unsigned char pointer. For this i need to convert the BITMAPINFO struct into a unsigned char *

So the new buffer will be,

unsigned char * pCompleteBMPIMageData = pBMPHeaderData + pRawBMPData;

how to do this?

View 3 Replies View Related

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 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++ :: Which Object Draw Is Being Called In Case Of Pointer Pointing To NULL

Apr 12, 2013

[URL] ....

Which object's draw is being called?

View 6 Replies View Related

C++ :: Virtual Function Table Pointer Misplaced In Object Memory

Jul 29, 2014

I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.

Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.

Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?

///////////////////////////////////////////////////////////////////////////
"MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info
188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114
189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128
190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128

[Code] .....

View 4 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++ :: Object That Is Passed To Function Is Changed Although No Pointer Is Passed

Mar 22, 2013

I am posting this simplified piece of code that is a bit confusing for me. There are two functions that I call. One shows the expected results but the result of the other one rather puzzles me.

//#define defineVecTyp Vec3f
#define defineVecTyp float
template <typename vecTyp>
vecTyp buildLaplacianPyramid(cv::Mat inputmat) {
vecTyp lapPyr;

[Code].....

Calling the function sum1 does not change the values stored in the variables val1 and val2. The output of the program is as follows:

val1= 1 ## val2= 10 // before the call of function sum1
val1= 1 ## val2= 10 // after the call of function sum1
sumOfVals= 22

This is quite obvious and as expected and I just pasted this piece of code as an example for better clarification.

However, if I call the function buildLaplacianPyramid and apply a function for Gaussian Blurring, this also effects the cv::Mat passed to the function. The line imshow("M1, after buildLaplacianPyramid",M1); therefore shows an image that is blurred. Since I am not passing a pointer to the cv::Mat I do not understand why this should be happening. I was assuming that there would be a copy of the cv::Mat M1 to be used within the function. Therefore I was expecting the cv::Mat M1 to retain its original value. I was expecting that all changes applied to cv::Mat inputmat within the function would not have any influence on the cv::Mat M1. Just like in my other example with the sum.

View 3 Replies View Related

C++ :: Class Template Convert Which Can Convert Itself To Any Data Type

Sep 23, 2012

Code:
template<class T>
class Convert {
T data;
public:
Convert(const T& tData = T()) : data(tData)

[Code] ....

Why do we use operator? Is float and double function names below?

Code:
Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

View 1 Replies View Related

C++ :: Will Copy Constructor Does Object Initialization Using Another Already Created Object

Mar 16, 2013

will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?

View 10 Replies View Related







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