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


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++ :: 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++ :: 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 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++ :: Operator Overloading And Exception Handling

Nov 15, 2013

I have a date class and i overloaded operator >> to accept input in dd/mm/yyyy format. if i enter the wrong date format my program will crash. How do i do exception handling for this? How should i do the try part? and for catch, I'll just catch a date class variable?

Code:
void operator >> (istream &is, clsDate &date) {
string inputDate;
is >> inputDate;
int mm = stringToNumber(inputDate.substr(3,2)); // read 2 characters from character number 3 start
int dd = stringToNumber(inputDate.substr(0,2)); // read 2 characters from character number 0 start
int yy = stringToNumber(inputDate.substr(6,4)); // read 4 characters from character number 6 start

[Code] .....

View 2 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++ :: 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++ :: 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++ :: Namespaces / Classes - Perform Operator Overload With Insertion Operator

Mar 22, 2013

I'm doing a refresher for C++ and have gotten to operator overloading. I'm trying to perform an operator overload with the insertion (<<) operator, but I have encountered a problem.

Here's my class [In a header file "Shinigami.h"]

#include<string>
namespace K{
class Quincy;
class Shinigami{
friend std::ostream& operator<<(std::ostream&, const Shinigami&);

[Code] .....

If the operator function is a friend of the 'Shinigami' class, why doesn't it recognize any of it's private members? I need it to be in this file because I'm doing a bit of association with the 'Quincy' class.

I thought it was the namespace, but I included that.

View 9 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 :: Add Little Extra To Switch Statement Assignment

Jun 12, 2013

I pretty much got the assignment done. All it asked for was that you make a C program that displays which manufacturer owned a disk drive based on the code entered. Pretty simple. Just enter 1, 2, 3, or 4 and get the associated manufacturer. Though I am trying to implement an error messege to it for any interger that isn't 1-4.

Code: #include <stdio.h>
int main()
{

[Code]...

o errors are given and it works fine as long as you enter 1-4, but when you enter any other didgit it just stops the program without any messeges.

View 5 Replies View Related

C :: Error - Incompatible Types In Assignment

Oct 11, 2013

I am working on a double linked list and inside of my function to insert a node, I am getting an error of "Incompatible types in assignment". Here is my function code. Line 55 is where I am receiving the error.

Code:

45 struct lnode *ins_llist(char data[], struct llist *ll){
46 struct lnode *p, *q;
47
48 q = malloc(sizeof(struct lnode));
49 if ( q == NULL )

[Code]....

View 2 Replies View Related

C++ :: Why Assignment Makes Pointer From Integer

Jul 27, 2013

when i compile the following program i get a compiler warning, but i don't understand why. for me the code seems to be all right and does legitimate this warning. so here is the code

// multiplication.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "crypto.h"
#define PROGNAME "multiplication"
void usage();
int isnumaber(char* str);

[code]....

View 2 Replies View Related

C++ :: Member Function Pointer Assignment?

May 15, 2013

I have a class with member functions and a pointer like this:

Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};

Now in a regular function I create an instance of the class and try to assign the pointer:

Code:
MyClass mc;
mc.fnptr = &mc.fn1;

The compiler does not like it and suggests this instead:

Code:
mc.fnptr = &MyClass::fn1;

This seems to work but what if I have two instances of the class:

Code:
MyClass mc1, mc2;

How does the compiler know to distinguish between

Code:
mc1.fn1

and

Code:
mc2.fn1

when the assignment now looks identical:

Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;

View 14 Replies View Related

C :: Error - I Value Required As Left Operand Of Assignment

Apr 21, 2013

I am getting this error when compiling my program with quincy:

Error: I value required as left operand of assignment

The program is meant to calculate how much parking costs based on the amount of hours in a park and what type of vehicle it is. the error is coming from my function definitions which i have just started to add in.

Code:
float calcCarCost (char vehicletype, int time, float car)
{
if ((time > MINTIME) && (time <= 3))
calcCarCost =( CAR * time );
}

The error is on line 72 which is:
calcCarCost =( Car * time);

I should probably point out CAR is already defined as a constant with a numerical value given and time is previously asked to be input in when the program runs.

View 10 Replies View Related

C/C++ :: Warning / Assignment From Incompatible Pointer Type

Apr 17, 2014

I'm working on a program and everything works except for the follow function:

void swapHex(int x, int byte1, int byte2) {
unsigned char *b1, *b2, tmpc;
printf("%d in hex is %x
", x, x);
printf("swapping byte %d with byte %d
", byte1, byte2);

[Code] ....

I get the following errors when compiling:

In function "swapHex":
warning: assignment from incompatible pointer type
warning: assignment from incompatible pointer type

View 2 Replies View Related

C/C++ :: Invalid Lvalue In Assignment While Trying To Swap Pointers

Apr 1, 2013

While executing this code i was getting a error Invalid lvalue in assignment. Can any one tell how to correct this.

dataItem* d=(dataItam*)malloc(10*sizeof(dataItem));
dataItem* temp;
temp=(d+6);
(d+6)=(d+8);//error line
(d+8)=temp;//error line

View 3 Replies View Related

C++ :: Exception Seems To Materialize Out Of Nowhere

Jan 30, 2014

My perceptron's train function seems to materialize my BadInput exception object out of nowhere. Yet, everything works fine.

And when I put an forever loop in main to validate the output with many different inputs, I get a bad_alloc exception and a call to terminate().

BadInput.hpp

Code:
#ifndef BADINPUT_HPP
#define BADINPUT_HPP
class BadInput {
public:
BadInput(int val):difference(val) {

[Code] ....

I cannot figure out why train() is throwing an exception...feedforward didn't, as it's error message never printed.

View 3 Replies View Related

C :: Lvalue Required For Left Operand Of Assignment Error

Oct 18, 2013

The issue arises with case 3 where it tells produces an error when I attempt to compile and says "lvalue required for left operand of assignment error". How to fix this so that I can properly run the program.

View 5 Replies View Related

C++ :: Error - LValue Required As Left Operand Of Assignment

Jan 1, 2014

I get the following error:

pointers_array.c: In function ‘readlines’:
pointers_array.c:42:37: error: lvalue required as left operand of assignment

I know what the error is saying but what it is giving it with this code:

/* readlines: read input lines */
int readlines(char *lineptr[], int maxlines) {
int len, nlines;
char *p, line[MAXLEN];
nlines = 0;

[Code] ....

View 3 Replies View Related

C++ ::  In Function Assignment Makes Pointer From Integer Without A Cast

Jul 4, 2013

The log file gives me: In function ‘memFileAlloc’ assignment makes pointer from integer without a cast..When compiling the drivers for the Matrox card in the DL580. The offending code is:

STACK_LINKAGE MEMHANDLE memFileAlloc(
UINT32 dwSize,
const char* pszFileName,
int iLine) {
void* pvChunk;
#if MEMORY_STATS

[code]...

I think the offending line is:
pvChunk = ClientMemAlloc(dwSize + sizeof(UINT32), NULL)
because that's what the log file tells me.

The system is a 16 core HP DL580 G4 with 8g RAM, RAID 0, Mandrivalinux 11.0 and the display is a Matrox Parhelia 256PCIx.

View 11 Replies View Related







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