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
ADVERTISEMENT
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
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
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
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
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
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
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
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
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
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
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
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
Jul 26, 2012
Try to implement overloading << operator. If I done it void then everything work fine (see comment out) if I make it class of ostream& then the operator return to me some memory address.
Code:
#ifndef Point_HPP // anti multiply including gates
#define Point_HPP
#include <sstream>
class Point {
private:// declaration of private data members
double x;// X coordinate
double y;// Y coordinate
[Code] .....
View 7 Replies
View Related
Dec 2, 2014
What is another way I could convert string to int in this overloaded operator? This way gives me an error.
Code:
istream &operator>>(istream& in, MasterData& d) {
string value;
getline(in, d.playerId, ',');
getline(in, d.firstName, ',');
getline(in, d.lastName, ',');
[Code] .....
View 4 Replies
View Related
Mar 13, 2013
how can we use modulus operator with double type variables
View 2 Replies
View Related
May 27, 2013
Say I wanted to overload the modulus operator to return the remainder of a division between two floating point numbers. Why isn't a custom double operator%(double, double) allowed even though that function isn't available in the standard anyway?
View 5 Replies
View Related
Apr 25, 2014
Code:
returnType operatorOperatorSymbol (parameterList); // syntax for operator overloading
Class Fraction
public:
Fraction operator-(const Fraction& f1){
Fraction r;
return r;
}
Is this even syntactically correct? It gives me errors. Im just trying to compile it without errors. I think the function makes sense since its returning a type Class
View 2 Replies
View Related
Feb 26, 2015
understand the below program.
Why I'm getting output y is 6.000000
Code:
#include <stdio.h>
int square(double a);
int main()
[Code].....
View 6 Replies
View Related
Nov 21, 2012
Say I have overloaded functions with different arguments AND return types. Each set of argument has only one corresponding return type.
Code:
Vector grad(Scalar)
Tensor grad(Vector)
Later I have:
Code:
template <class T>
void test(T x) {
... Y = grad(x)
}
Then how do I automatically declare the type of Y. Do I need to make the grad function a template and specialize each of them ?
View 4 Replies
View Related
May 17, 2014
while (getline(inStream, line))
{
while (inStream >> Student.getId() >> Student.FNAME >> Student.MINIT >> Student.LNAME >> Student.GENDER >> Student.UNITS >> Student.getGpa())
{
while (Student.getId() != id)
{
outStream << line << endl;
}
}
}
This is what I have right now. It shouldn't be a problem, but for some reason I am getting an error trying to >> Student.getGpa()
Error1error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)c:location1301Project 5
I will post more code if needed, but... I just don't know. I have a TON of code so I would rather not if I don't have to.
View 2 Replies
View Related
Nov 13, 2014
I cannot get my function overloading the input operator for rational type objects to work.
lab9.cpp: In function ‘std::istream& operator>>(std::istream&, const rational&)’:
lab9.cpp:186:20: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from expression of type ‘rational’
return (inputFrac);
lab9.h
#include <iostream>
using namespace std;
class rational {
public:
rational();
rational(int a, int b);
[Code] ......
View 4 Replies
View Related
Apr 29, 2014
I am new to c++ and trying to learn. for instance. i have a struct and method.I am trying to learn what i can do with the method if i define the return type as struct type.
struct S {
int age;
string name;
};
S method() {
//what i can do in here. with the Struct. I mean can i reach members of the struct. etc
}
View 2 Replies
View Related
Apr 7, 2014
In my main I have a do/while loop, which is to iterate around the method calculateImportance while i is not equal to the numNodes.
do
{
int i;
int numNodes = Test.count();
for(i=0; i<=numNodes; i++)
[Code]....
I have been messing around with the data type node, hence why it is there are present, but, that doesn't seem to the answer
This is the function declaration in the header file.
node CalculateImportance();
View 1 Replies
View Related
Feb 25, 2014
i'm getting a new error is a wrong return type.
WebAPI.cs
namespace DarkAPP___WForm.Libs {
class WebAPI {
System.Net.WebClient wc = new System.Net.WebClient();
public WebAPI() {
[code].....
View 6 Replies
View Related
Jan 17, 2013
i heard that it was int but later it is deprecated , so at present what is the default return type of a function ?
View 4 Replies
View Related