assignment-operator

Why are we allowed to change values of “const” qualified variables?Why pointers are allowed for this,but not assignment?

大城市里の小女人 提交于 2019-12-19 09:01:22
问题 Consider the following 2 programs prog1 and prog2 .Here if I try to change the value of the const qualified variable i using a pointer ptr ,I get the warning( not error) "initialization discards qualifiers from pointer target type|" ,but the program runs nevertheless and displays the new value.But if I try to change the value of i in the second program using an assignment statement,I get the error (not warning) assignment of read-only variable 'i'| . Here are confusions arising from this

Java - comma operator outside for loop declaration

此生再无相见时 提交于 2019-12-19 02:47:05
问题 I know I can use the comma operator like this for (int i = 1, j = 15; j>10; i++, j--) { // do something neat } but some articles seem to suggest that the comma operator can be used outside of the for loop declaration, for example int j = 2, k = 4 ; int x ; // Assignment statement with comma operator x = j + 1, k ; source: http://www.cs.umd.edu/~clin/MoreJava/ControlFlow/comma.html or int x = (expression) ? (i++,2) : 3; source: https://stackoverflow.com/a/12047433/1084813 This would be a neat

Assignment operator not available in derived class

≯℡__Kan透↙ 提交于 2019-12-18 18:51:17
问题 The assignment operator in base class does not seem to be available in derived class. Given this code: #include <iostream> class A{ int value; public: A& operator=(int value){ this->value = value; return *this; } }; class B : public A{}; int main(){ B b; b = 0; // Does not work return 0; } GCC 6.4 says: error: no match for 'operator=' (operand types are 'B' and 'int') What is happening? 回答1: Every class has at least one assignment operator implicitly defined when we don't provide one

How to approach copying objects with smart pointers as class attributes?

强颜欢笑 提交于 2019-12-18 16:47:11
问题 From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also

How to approach copying objects with smart pointers as class attributes?

老子叫甜甜 提交于 2019-12-18 16:47:10
问题 From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also

Why does virtual assignment behave differently than other virtual functions of the same signature?

老子叫甜甜 提交于 2019-12-18 11:47:12
问题 While playing with implementing a virtual assignment operator I have ended with a funny behavior. It is not a compiler glitch, since g++ 4.1, 4.3 and VS 2005 share the same behavior. Basically, the virtual operator= behaves differently than any other virtual function with respect to the code that is actually being executed. struct Base { virtual Base& f( Base const & ) { std::cout << "Base::f(Base const &)" << std::endl; return *this; } virtual Base& operator=( Base const & ) { std::cout <<

Get name of x when defining `(<-` operator

筅森魡賤 提交于 2019-12-18 03:45:33
问题 I want to define (<- and access the name of the left hand side argument : *<- functions use internally an intermediate '*tmp*' variable. Is it still possible to get the name of x ? `(<-` <- function(x,value){ print(deparse(substitute(value))) print(deparse(substitute(x))) print(match.call()) value } foo <- 0 (foo) <- 3 # [1] "3" # [1] "*tmp*" # `(<-`(x = `*tmp*`, value = 3)# [1] "3" I want to get "foo" from inside the function. I tried to hack it by using tracemem , i.e. calling sapply(ls

Array type char[] is not assignable [duplicate]

烂漫一生 提交于 2019-12-17 22:24:31
问题 This question already has answers here : problems with char array = char array (2 answers) Closed 4 years ago . Well here is my first post. I've been trying to do this choice choosing thing and I want the user to choose only numbers instead of typing them down (easier) but when I want the numbers to equal a string, it says "array type char[30] is not assignable". Even if at the back I put semi-colon or not. #include <stdio.h> int main() { int choice1; char word[30]; printf("You have three

Is a += b more efficient than a = a + b in C?

自古美人都是妖i 提交于 2019-12-17 19:16:10
问题 I know in some languages the following: a += b is more efficient than: a = a + b because it removes the need for creating a temporary variable. Is this the case in C? Is it more efficient to use += (and, therefore also -= *= etc) 回答1: So here's a definitive answer... $ cat junk1.c #include <stdio.h> int main() { long a, s = 0; for (a = 0; a < 1000000000; a++) { s = s + a * a; } printf("Final sum: %ld\n", s); } michael@isolde:~/junk$ cat junk2.c #include <stdio.h> int main() { long a, s = 0;

C++ function returns a rvalue, but that can be assigned a new value?

落花浮王杯 提交于 2019-12-17 18:57:59
问题 The code is as follows: #include <iostream> using namespace std; class A { }; A rtByValue() { return A(); } void passByRef(A &aRef) { // do nothing } int main() { A aa; rtByValue() = aa; // compile without errors passByRef(rtByValue()); // compile with error return 0; } The g++ compiler gives the following error: d.cpp: In function ‘int main()’: d.cpp:19:23: error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’ d.cpp:12:6: error: in passing argument 1 of