Accessing Public Class Function in Private Class Giving Error - c++

class test
{
private:
class privateStruct
{
public:
int m;
privateStruct(int p){m=p;}
};
};
void ff()
{
test::privateStruct ps(4);
throw ps; //Does not work..
}
void main()
{
try
{
ff();
}
catch(...)
{
}
}
But the Following Code Works Why
class test
{
private:
class privateStruct
{
public:
int m;
privateStruct(int p){m=p;}
};
};
void ff()
{
throw test::privateStruct(4); //Work why
}
void main()
{
try
{
ff();
}
catch(...)
{
}
}
NOTE : I am using VC++ 6.
I need answer why the above code works.
Thanks in Advance :)

This is an old/known bug with Visual Studio 6.0. It ignores access specifiers when constructing temporaries. No fix is available.
Raising the warning level to 3 or higher (/W3) will cause the offending code to give a warning.

The code in your second example works because Visual C++ 6 is notorious for its horrible standards compliance.
It works by accident.

Even the second code snippet won't compile. privateStruct cannot be accessed in the function ff().

Related

Pointer to a different instance.

How can such a code work correctly when the IWindow pointer clearly has an address to a ISheet class which has no method Say?
#include <iostream>
using namespace std;
class IWindow
{
private:
int p;
double f;
public:
void Say() { cout << "Say in IWindow"; }
};
class ISheet
{
public:
void foo() { cout << "ISheet::foo"; }
};
int main()
{
ISheet *sh = new ISheet();
int ptr = (int)sh;
IWindow *w = (IWindow*)ptr;
w->Say();
sh->foo();
return 0;
}
When compiled in Visual Studio 2015 it runs and executes with no problems, but I was expecting to get an error on line w->Say(). How is this possible?
It works by the grace of the almighty Undefined Behavior. Your functions don't try to access any data members of the containing class, they just write something to std::cout, which anyone can do.
What you've effectively done is
#include <iostream>
void IWindow_Say(void*)
{
std::cout << "Say in IWindow";
}
int main()
{
IWindow_Say(0xdeadbeef); // good luck with that pointer
}
You never used the pointer (which became this in your original example) so no side-effects were observed.

Why do I get C2883: function declaration conflicts with - introduced by using-delcaration when overriding only one overload from a base class?

Why does the following code give me a compiler error?
struct lol {
void foo(int hi) { }
void foo(lol x) { }
};
void funcit() {
struct duh : public lol {
using lol::foo;
void foo(lol x) { }
};
lol().foo(10);
lol().foo(lol());
duh().foo(10);
duh().foo(lol());
}
int main() {
funcit();
return 0;
}
I would expect it to compile, wherein duh::foo would call lol::foo - only overriding one of the overloads. Using Visual Studio Express 2012, I get this error:
error C2883: 'funcit::duh::foo' : function declaration conflicts with 'lol::foo' introduced by using-declaration
The code is correct and compiles with GCC 4.8.1. This appears to be a bug in MSVC 2012. Lifting the struct out of the function will cause it to work properly:
struct lol {
void foo(int hi) { }
void foo(lol x) { }
};
namespace {
struct duh : public lol {
using lol::foo;
void foo(lol x) { }
};
}
void funcit() {
lol().foo(10);
lol().foo(lol());
duh().foo(10);
duh().foo(lol());
}
int main() {
funcit();
return 0;
}

C++ help writing a class with declaration on header file

Well, hi there.
I'm new to c++ and I'm having some issues that I'm not sure what is causing them.
This is my main:
#include "GameWindow.h"
int main(void)
{
GameWindow * game_window = new GameWindow(true);
/* loop the game */
while (game_window->GetRunning())
{
// update
game_window->Update();
// draw
game_window->Draw();
}
delete game_window;
return 0;
}
and this is my header:
class GameWindow
{
private:
bool _running;
//GLFWwindow* _window;
public:
void SetRunning(bool new_val);
bool GetRunning();
GameWindow(bool running);
void Draw();
void Update();
}
and my c++ file:
#include "GameWindow.h"
void GameWindow::SetRunning(bool new_val)
{
_running = new_val;
}
bool GameWindow::GetRunning()
{
return _running;
}
GameWindow::GameWindow(bool running) :
_running(running)
{
}
void GameWindow::Draw()
{
}
void GameWindow::Update()
{
}
While going through all of this I find it tough to find why Visual Studio refuse to compile this code.
It's raising errors about how 'SetRunning' is overloading a function which differs only in return values, and that the return type of main should be Int and not GameWindow, and with all of this I just went completely lost.
Tried to put 'SetRunning' as a comment to simplify the issue but instead it raised the same on 'GetRunning' instead.
I'm guessing it's a really stupid mistake that is easy to fix, but still, can't find it.
Thank you for your time, and I'll appreciate any kind of help.
Missing ; at the end of class definition.
class GameWindow
{
// .....
}; // Missing semi-colon
Missing ; in class defination
{
};
because of this when you include the file in program then compiler not found the end of the class hence it says return type of main should be int not GameWindow

Getting an undefined reference error

I am hoping someone can point me in the right direction to figure out why I am getting the following error:
$~/display/triangleDisplayable.cc:4: undefined reference to `Displayable::Displayable()'
I am trying to abstract a class Displayable and have a class triangleDisplayable that implements its methods. The two header files I have are "Displayable.h":
class Displayable {
public:
Displayable();
virtual int getSizeOfArrays() = 0;
void display(int size);
private:
virtual void init() = 0;
virtual int getSizeOfPointsArray() = 0;
virtual int getSizeOfNormalsArray() = 0;
};
and "triangleDisplayable.h"
#include "Displayable.h"
class triangleDisplayable : public Displayable
{
public:
triangleDisplayable();
int getSizeOfArrays();
private:
void init();
int getSizeOfPointsArray();
int getSizeOfNormalsArray();
};
And then I have "Displayable.cc"
#include <iostream>
#include "Displayable.h"
Displayable::Displayable() {
std::cout << "testing Displayable constructor" << std::endl;
}
void Displayable:display(int size) {
}
int main () {
return 0;
}
and "triangleDisplayable.cc"
#include <iostream>
#include "triangleDisplayable.h"
triangleDisplayable::triangleDisplayable() : Displayable() {
}
int triangleDisplayable::getSizeOfArrays() {
return 0;
}
void triangleDisplayable::init() {
}
int triangleDisplayable::getSizeOfPointsArray() {
return 0;
}
int triangleDisplayable::getSizeOfNormalsArray() {
return 0;
}
int main () {
return 0;
}
I have been trying to follow along with various tutorials to learn how to do abstraction in C++, but I have not really been able to find any helpful solutions to this. I believe that all of my #includes are correct, which I read is a common problem. The error message seems to indicate that the problem is the line
triangleDisplayable::triangleDisplayable() : Displayable() {
}
I have tried to compile without the : Displayable() but I get the same error. Is there perhaps a problem with my syntax in my header files?
No, the error is in tool invocation. You need to link the two source files together (e.g. g++ -o foo a.cc b.cc). And remove one of the main functions, as you can't have two different ones.

Equivalent to toString() in Eclipse for GDB debugging

In Eclipse I can override the toString() method of an Object to pretty print it. This is especially useful because during a debug sessions as I can click on a variable and see the object in a human readable form.
Is there any kind of equivalent for C++ during a gdb session. I'm also open to any IDEs that can emulate this behavior.
In gdb, print command prints the contents of the variable. If you are using any IDE for C++, eg. Netbeans, Eclipse, VC++ then pointing on the variable shows the content.
EDIT: See if the below code is what you are looking for.
#include <string>
using std::string;
#define magic_string(a) #a
template<typename T>
class Object_C
{
private:
virtual string toString_Impl()
{
return magic_string(T);
}
public:
Object_C(void)
{
}
virtual ~Object_C(void)
{
}
string toString()
{
return toString_Impl();
}
};
class Base_C :
public Object_C<Base_C>
{
private:
string toString_Impl()
{
char str[80] = "";
sprintf_s(str, 79, "Base.x:%d\n", x_);
return string(str);
}
private:
int x_;
public:
Base_C(int x = 0) : x_(x) { }
~Base_C(void);
};
void ToStringDemo()
{
Base_C base;
cout << base.toString() << endl;
}
using visual Studio C++ instead?
[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
public int count = 4;
}
https://learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/enhancing-debugging-with-the-debugger-display-attributes