C++ :: Assignment Operator For A Class With Reference

Mar 6, 2014

Class A
{..........}

Class B:
{....
private U& u;}

I need to write the copy constructor and assignment operator for Class B above. Copy would look something like this:

B::B(conts B& bo): u(bo.u){}

is this correct ... and how will the assignment operation look like??

View 3 Replies


ADVERTISEMENT

C++ :: Why Assignment Operator Return By Reference

Feb 1, 2015

I have code here that uses assignment operators that doesn't return by reference and it still works. So why does my book say you need to return by reference?

Here is a quote from my book:

The return type of operator= is a reference to the invoking object, so as to allow chained
assignments a=b=c.

The code below is from my book. I simply removed '&', in the original code that has assignment operators return by reference, from IntCell & operator=. This way the assignment operator no longer returns a reference, and it still works.

#include <iostream>
using namespace std;
class IntCell {
public:
explicit IntCell( int initialValue = 0 )
{ storedValue = new int{ initialValue }; }

[Code] .....

View 3 Replies View Related

C++ ::  Overloaded Assignment Operator For Class Template Objects?

May 23, 2013

I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.

Here is the class definition:

template <class dataType>
class myArray {
public:
void setArrayData();

[code]....

And here is the definition of the overloaded assignment operator:

template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) {
int i;
if(this != &rightArray) {
delete [] arrayPtr;

[Code] ....

And here is my main function that tests the operations on objects of the class:

int main(){
//object declarations
myArray<double> list(5); //a single-parameter object declaration of class myArray
myArray<double> myList(2,13); //a two-parameter object declaration of class myArray

[code]....

The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!

View 19 Replies View Related

C++ :: Create Assignment Operator For A Class That Uses Pointer For Its Private Variable?

Mar 30, 2013

i am trying to create the assignment operator for a class that uses a pointer for it's private variable. The error is saying expected constructor, deconstructor, or type conversion before "operator. (which is the assignment operator. I have tried everything i could think of or find online and nothing has worked. below is the code for the assignment operator in the .h file and the .cpp file.

//Assignment constructor
indexList &operator=(const indexList <T> &rhs);
template <class T>
indexList<T>::indexList operator=(const indexList <T> &rhs) {
if(this != &rhs) {
numberOfElements = rhs.numberOfElements;

[Code]...

View 1 Replies View Related

C++ :: Return Type For Assignment Operator

Apr 7, 2014

I am wondering why return type for an assignment operator cant be a void or int? Cant I write assignment operator for student class like this as we do nothing with returned value?

Student {
char name[20];
int marks;
public:
student(char*name,int marks)

[code].....

View 2 Replies View Related

C++ :: Assignment Operator But With Some Member Exceptions

Jan 9, 2015

The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':

#include <iostream>
#include <string>
struct Person {
std::string name;
int ID, age, height, weight;

[Code] .....

Name = Bob
ID = 2047
Age = 38
Height = 183
Weight = 170

Name = Frank
ID = 5025
Age = 25
Height = 190
Weight = 205

Bob pretends to be Frank, but keeps his name and ID.

Name = Bob
ID = 2047
Age = 25
Height = 190
Weight = 205

But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.

View 3 Replies View Related

C++ :: Copy Constructor And Assignment Operator?

Feb 3, 2014

How do i write main test program to test the copy constructor and assignment operator in this program...how do i know if its working as its suppose to?i just want to know about copy and assignment operator..i have figured out the test program for other things..Here my program :

ListType.h

#ifndef LISTTYPE_H
#define LISTTYPE_H
#include<iostream>
class ListType{
public:
ListType(size_t=10);
ListType(const ListType&);

[Code] ......

View 1 Replies View Related

C++ :: Copy Constructor And Assignment Operator

May 20, 2013

I've been working on some project and I got to wondering when you know you need to use a copy constructor and an assignment operator. Is there a rule of thumb? I know there is the Rule of Three, but is there something that tells you when you need those three?

View 4 Replies View Related

C++ :: Exception Proof Assignment Operator

Apr 11, 2014

Below is exception proof approach of assignment operator shared by scott meyer. Is it safe to delete the raw pointer.

int *orig =m_p;
m_p=new int (*obj.m_p);
delete orig;

View 1 Replies View Related

C++ :: Declaration Of Copy Constructor And Assignment Operator

Sep 13, 2013

Go to this link : [URL] .....

Actually I am not getting one thing in this code that why they only provide the declaration of Copy constructor and Assignment operator...??

View 1 Replies View Related

C++ :: Copy Assignment Operator Method Calling?

Nov 12, 2014

how the operator overloaded methods called in C++?

Class String {
int len;
char *str;
public:
String(const char *str1="") {
len=strlen(str1);
str=new char[len+1];
strncpy(str,str1,len+1);

[code]....

View 1 Replies View Related

C++ :: Error When Using Volatile Object In Overload Assignment Operator

Jul 27, 2012

/*using GENERIC_COMMAND* A; as volatile generates error. but here i have to use union object as volatile i.e. volatile GENERIC_COMMAND* A; */

#include <iostream>
using namespace std;

typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
union GENERIC_COMMAND {

[Code] .....

View 14 Replies View Related

C++ :: Passing String Using Reference Operator

Aug 28, 2014

Why it is necessary to use the reference operator here ?

const string& content() const {return data;}

Example in [URL] ....

// classes and default constructors
#include <iostream>
#include <string>
using namespace std;

class Example3 {
string data;

[Code] .....

View 2 Replies View Related

C++ :: Overloading Binary And Assignment Operators In Vector Class

Feb 5, 2013

I am making a vector class and am having some problems creating the overloaded arithmetic operators and assignment operators.

Here is an example of my "+=" code as it stands the errors are similar/the same for the other operators except "=" operator which works fine:

Vector3& Vector3::operator+=(const Vector3 &rhs) {
Vector3 tmp = *this;
Set(tmp.getX() + rhs.getX(), tmp.getY() + rhs.getY(), tmp.getZ() + rhs.getZ());
return *this;
}

I have tried a lot of different approaches ad always get the error:

error: passing 'const Vector3' as 'this' argument of 'double Vector3::getX()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getY()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getZ()' discards qualifiers

View 5 Replies View Related

C++ :: Extractor Overloading And Member Wise Assignment For Cartesian Class

Apr 6, 2014

I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output Please enter the first coordinates: Please enter the second coordinates:

(4.86129e-270, -1.97785e-41)
(4.86143e-270, -1.97785e-41)

Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

[Code] ....

Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so?

View 7 Replies View Related

C++ :: Derived Class Inherit Operator Of Base Class

Jan 17, 2012

If Yes, then why this syntax does not works :

class Derived : public Base {
public:
Derived& operator=(const Derived &rhs) {
operator =(static_cast<const Base&>(rhs));

[Code] ....

View 2 Replies View Related

Visual C++ :: Pointer / Reference To Class Within Class?

Oct 3, 2012

I have encountered a problem I can't see to solve. I want to access a function and can't seem to find the right combination to get me there. Here is what I am looking at:

CFoo1::CFoo2::GetStrDataC(int nRow) const

How do I call the GetStrDataC function from another class?

View 6 Replies View Related

C++ :: Class Using New Operator

Jan 5, 2015

Is it usual to rely completly on the new operator in constructors/copy constructors. What if new trows an exception? The application ends and that's it? The new operator can be placed where it can't be catch like in constructor initialization list. What kind of practice I should adopt when using "new" in those cases?

The sample code below is taken from here... [URL] ....

class MemoryBlock {
public:

// Simple constructor that initializes the resource.
explicit MemoryBlock(size_t length)
: _length(length)
, _data(new int[length])

[Code] .....

View 9 Replies View Related

C++ :: How To Use The Operator In Class Template In Program

Apr 25, 2013

I have a list of numbers in an array created by a class template

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int CAPACITY = 20;
template <class T>
class A

[code].....

View 2 Replies View Related

C++ :: Operator Overloading In Inherited Class

Jun 2, 2013

So i am having troubles with operator overloading in inherited class. Basically, it doesnt work. Consider this:

Code:

class A {
public:
A() {
x=0;
z= new int;

[Code] ....

Some how the copy constructor of a is improperly executed - the pointer is copied over, not re-created. As a result, the destructors crashes due to double-free.

*/
B bb = b; //doesnt work
B bbb(b); //doesnt work
B bbbb(b, 0); //works
}

Above code shows the problem well. The "official" copy-constructor wont work - it copies over the pointer directly, and doesnt create a new one as it should. However, if i provide my own pseudo-copy-constructor that works. But ofcourse it's just a cheap work around - and wont actually work in real code (STL).

compiler is VS2008SP1.

View 11 Replies View Related

C++ :: Can Avoid Class Using Array Operator?

Feb 7, 2014

Imagine these simple class:

Code:
class test {
public:
void write(string a) {
cout << a;
}
};

(these class wasn't tested... it's for these question)... Can avoid the class use the array operator('[]')?

View 7 Replies View Related

C++ :: Defining Operator In Abstract Class

Jun 22, 2012

I have an abstract class called Mbase and from it derived two classes: Sparse and Dense. Now I have an array in which its elements can be either Sparse or Dense. So, I delcared the array to have pointers to Mbase class. For example:

PHP Code:
Mbase** A;
Sparse* A1 = new Sparse;
Dense* A2 = new Dense;
A[1] = dynamic_cast<Mbase*>(A1);
A[2] = dynamic_cast<Mbase*>(A2); 

Now, I have operator + defined in Sparse and Dense. but when I do

PHP Code:

A[1]+A[2] 

I get that operator + is not defined for Mbase class. So, I tried to define it in the Mbase class

PHP Code:

class Mbase{
public:
void put()=0;
double get()=0;
Mbase operator +(Mbase A);


However, the last code does not compile complaining that it cannot declare a class of type abstract in Mbase operator +(Mbase A). I think this is because I am returning Mbase instance.

View 10 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++ :: Undefined Reference To Class Definition

Aug 4, 2013

I have successfully built OGDF under directory undefined reference to /home/vijay13/Downloads/OGDF-snapshot/

I have following code in test.cpp under directory /home/vijay13/Downloads/ :

#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>
using namespace ogdf;

[Code] .....

while compiling as following :

vijay13@ubuntu:~/Downloads$ g++ -o test test.cpp -I /home/vijay13/Downloads/OGDF-snapshot/include/

I am getting following error:

vijay13@ubuntu:~/Downloads$ g++ -o test test.cpp -I /home/vijay13/Downloads/OGDF-snapshot/include/
/tmp/ccPE8nCu.o: In function `main':
test.cpp:(.text+0x26): undefined reference to `ogdf::Graph::Graph()' ...................... so on

View 2 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++ :: Reference Semantic For Pimpl Class?

Jun 18, 2012

I have a basic class with a pimpl, with agressive instantiation:

Code:
class MyClassImpl;
class MyClass {
public:
MyClass() : pImpl(new MyClassImpl) {}

[Code] ....

Classic/Standard. However, I do hate having to use pointer semantics for all of my operations. So I thought: If I never ever manipulate the actual pointer itself, why not just keep a reference?

Code:
class MyClassImpl;
class MyClass {
public:
MyClass() : impl(*new MyClassImpl) {}
~MyClass(){delete &impl;}
private:
MyClassImpl& impl;
};

The only downside that I see, is that I can't swap or move, but this particular object is not meant to be swapped or moved.

View 4 Replies View Related







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