Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This code runs fine, when it should have a run-time error, since i haven't instantiated a derived class object.
#include <iostream>
using namespace std;
class Person {
public:
void walk() { cout << "person walking" << endl; }
};
class Employee :public Person {
public:
void work() { cout << "employee working" << endl; }
};
void main() {
Person* p = new Person();
Employee* e = static_cast<Employee*>(p);
e->work();// this is working - but why? it should fail at runtime
}
If static_cast only casting the pointer, how is it possible to call a child member function?
At what point is the child instantiated?
Is static_cast also instantiating objects?
No.
Your assertion that your code should "crash at runtime" is, unfortunately, wrong. Your code exhibits undefined behaviour meaning that it could do literally anything. In this case I expect it works because the address of the function is the same in both objects but really, it could be for any reason.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is the class.
#include <iostream>
#include <string>
std::string strName = "ABC";
class BlueOut
{
public:
void printName() { std::cout << strName << std::endl; }
};
Now i create a object of this class
BlueOut blueout;
And i call the function printName() of the object in lambda
auto a = [&]() { blueout.printName(); };
But the function does not gets executed.
In this line,
auto a = [&]() { blueout.printName(); };
the part [&]() { blueout.printName(); } is called a lambda expression. You bind it to some variable a. Now you have a function object a created by a lambda expression. In order to see the effect, this has to be invoked:
a();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have started doing C++ clases for over 2 weeks now and I have a small problem that i dont know how to solve. I have to make a simple Class with one int parameter. That class needs to work with direct and copying initilaization and needs to print message which initialization is used. For example:
TestClass T(60) //this should print a message "Direct initialization"
TestClass T = 205 // this should print a messagte "Copying initialization"
If you use a constructor with a single variable initialization, then this assignment operation will be done for you automatically. See my code below:
#include<bits/stdc++.h>
using namespace std;
class A {
int x;
public:
A(int xx) {
cout << "Constructor Called" << endl;
x = xx;
}
print() {
cout << "Value of obj is " << x << endl;
}
};
int main() {
A v = 10;
v.print();
}
Because you used initialization constructor, = operator was handled automatically. To prove that it is using the constructor, I used cout in constructor and created and called another printing function.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm reading through my C++ textbook for an upcoming class, and following along an exercise in the book. This exercise compiles and seems to give the results that you would expect, but it seems there is an error though, and I can't figure out how to fix it.
Here is the code.
// Page 706 from text
//Contents of ThisExample.h
class Example
{
int x;
public:
Example(int a){x=a;}
void setValue(int);
void printAddressAndValue();
};
/*
//Contents of ThisExample.cpp
#include "ThisExample.h"
*/
#include <iostream>
using namespace std;
/*********************************************************
* Set value of object.
*********************************************************/
void Example::setValue(int a) // <---------- Doesn't execute
{ // <---------- Doesn't execute
x = a; // <---------- Doesn't execute
} // <---------- Doesn't execute
void Example::printAddressAndValue()
{
cout<< "The object at address " << this << " has "
<< "value "<< (*this).x<<endl;
}
/*
//Contents of main program
#include <iostream>
#include "ThisExample.h"
using namespace std;
*/
int main()
{
Example ob1(10), ob2(20);
// Print the addresses of the two objects
cout<<"Addresses of objects are "<< &ob1 << " and "<<&ob2<<endl;
// Print the addresses and values from within the member function
ob1.printAddressAndValue();
ob2.printAddressAndValue();
return 0;
}
In the book, they talk about replacing
void Example::setValue(int a)
{
x = a;
}
with
void Example::setValue(int a)
{
this->x = x;
}
But when I step through it with a debugger (which I am also new to), I don't see that function ever getting called.
I've tried commenting out the function entirely and it still runs, that's how I know it isn't getting called.
I also tried removing from the class
Example(int a){x=a;}
but then it doesn't compile. Any help? I just want to move along with the textbook, which is called "Starting Out With C++ Early Objects
Judy Walters, Godfrey Muganda, Tony Gaddis" and the exercise is on page 706.
Thanks
It doesn't ever get called, because you never call it.
The only place that the member variable x is set, in this particular example, is in the constructor. And the constructor happens to do so directly, rather than by calling setValue().
You could later call setValue() to change x, but currently you do not.
It's not uncommon to provide functionality that makes sense to be part of the class, even if you're not using that functionality quite yet. Although, unless you're writing a library, you generally wouldn't do too much of writing functionality you don't yet need.
Perhaps later exercises in the textbook involve calling setValue(). I would just continue reading and not worry about this.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I started with working code (not written by me obviously), I saw them make a class object dataflash which made sense to me, but then after that they used:
DataFlash::ID id;
Which is obviously because they needed an object of that struct, but the fact they went back to the DataFlash class bugged me, not sure why, but I thought "No, no, you should be using the object we just made now" and promptly changed it to what I have below, which produces the following error:
error: invalid use of ‘struct main()::DataFlash::ID’
Well that's no fair, I'm basically doing the same thing to my eye, why is this invalid use? Are structs (and nested classes I assume too) useless once they are in an object?
#include <iostream>
using namespace std;
int main()
{
class DataFlash
{
public:
struct ID
{
uint8_t manufacturer; /**< Manufacturer id **/
uint8_t device[2]; /**< Device id **/
uint8_t extendedInfoLength; /**< Extended device information**/
};
};
DataFlash dataflash;
dataflash.ID id;
return 0;
}
The code defines several layers of nested scope, the original code is doing scope resolution with the scope resolution operator ::
Your example uses a member access operator . and the struct ID simply is not a member, it lives in the scope of your type, a type and value is not the same thing.
As a footnote, enumerations can be accessed in both ways due to enum's scoping rules.
#include <iostream>
struct object {
enum identifier {
a = 2,
b = 16,
};
identifier id;
};
int main(int argc, char* argv[]) {
object obj;
std::cout << object::a << std::endl;
std::cout << obj.b << std::endl;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Example
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
struct father
{
int variable;
father(){variable=0xEEEEEEEE;};
virtual void sing(){printf("trollolo,%x\n",variable);}
~father(){};
};
struct son:father
{
son(){variable=0xDDDDDDDD;};
virtual void sing(){printf("trillili,%x\n",variable);}
~son(){};
};
int main()
{
father * ifather=new(father);
son * ison=new(son);
father uncle;
father * iteachers;
*((long long*)&uncle)=0xDEAF;
iteachers=(father*)malloc(20*sizeof(father));
//ineffective assignments
iteachers[0]=*ifather;
uncle=*ifather;
ifather->sing();//called to prevent optimization
ison->sing();//only to prevent optimization
std::cout.setf(std::ios::hex);
std::cout<<"father:"<<*((long long*)ifather)<<","<<std::endl;
std::cout<<"teacher0:"<<*((long long*)&(iteachers[0]))<<","<<std::endl;
std::cout<<"uncle:"<<*((long long*)&uncle)<<","<<std::endl;
std::cout<<"(son:"<<*((long long*)ison)<<"),"<<std::endl;
// uncle.sing();//would crash
}
The vtable pointer of teachers[0] is zero when compiled with gcc.
Also the vtable pointer of uncle keeps its original value instead of being overwritten.
My questions: Why HAS it be that way?
Is there a CLEAN workaround? Can i go with uncle._vptr=ifather->_vptr and still be portable? What is the ORDINARY routine to copy an object? Should I even file a bug?
Note: it should copy the whole object platform-independant, because no matter how the identification of the object type is done, since it should always be inside the object's data block!
The article
Why does my C++ object loses its VPTr
didn't help me, that must have a different reason.
As I understand it, basically the question is whether this code:
#include <iostream>
using namespace std;
struct Base
{
virtual void sing() { cout << "Base!" << endl; }
virtual ~Base() {}
};
struct Derived: Base
{
void sing() override { cout << "Derived!" << endl; }
};
auto main()
-> int
{
Base* p = new Derived();
*p = Base();
p->sing(); // Reporting "Base" or "Derived"?
}
should report "Base" or "Derived".
In short, assignment does not change the type of an object.
Hence, it reports "Derived".