Friday, November 23, 2018

Digest for comp.lang.c++@googlegroups.com - 25 updates in 13 topics

Paul <pepstein5@gmail.com>: Nov 17 02:55AM -0800

Using a recent version of gcc, the below code (at end of post) doesn't compile.
Presumably, this is because "unsigned int" is interpreted as
"unsigned int int" which, I agree, does not make sense.
 
It seems to be a precedence problem.
std::cout << ( (unsigned int)(230)); works fine.
 
Presumably a C++ expert would know that the below code doesn't
compile without needing to try it.
Why does the compiler not recognise unsigned int as the type "unsigned"
in the below context?
 
Thanks,
 
Paul
 
 
#include<iostream>
int main()
{
std::cout << (unsigned int(230));
}
m.labanowicz@gmail.com: Nov 17 10:49AM -0800

Hi,
 
How to force gcc to complain about ambiguity.
 
Following there is example:
 
--{beg:main.cpp}---------------------------------
#include <iostream>
class A {
public:
A() : x(new int) { *x = 0; }
virtual ~A() { delete x; }
virtual void run() { std::cout << (void *)this << ":" << *x << std::endl; }
private:
int * x;
};
class B : public A {
public:
B(A & a) : a(a) { }
virtual void run() { a.run(); }
private:
A & a;
};
int main(void) {
A a;
 
A other;
B b(other);
 
a.run();
b.run();
 
A * ptrA = new B(a);
#if 1 // wrong
A * ptrB = new B(b); // <------ this is the point of issue
// simple allocation and copy Memory is executed
// instead of defined Constructor
#else // good
A * ptrB = new B(static_cast<A &>(b));

No comments: