Dziwne zachowanie kompilatora w Visual Studio 2013

1

Witam :D zauważyłem dzisiaj dziwne zachowanie kompilatora VS2013. Napisałem sobie taki kod:

#include <iostream>
#include <string>

using namespace std;

class TextBuilder
{
public:
	virtual void Draw(string Text) = 0;
};

class :public TextBuilder
{
public:
	void Draw(string Text) override
	{
		cout << "Text1: " << Text << endl;
	}
} Text1;

class :public TextBuilder
{
public:
	void Draw(string Text) override
	{
		cout << "Text2: " << Text << endl;
	}
} Text2;

void ShowString (TextBuilder &Method, string Text)
{
	Method.Draw(Text);
}

int main()
{
	ShowString(Text1, "Adam");
	ShowString(Text2, "Adam");
	return 0;
} 

Teoretycznie powinien on wyświetlić:

Text1: Adam
Text2: Adam

Ale wyświetla on tylko

Text2: Adam
Text2: Adam

Natomiast gdy zmieniłem kod na:

#include <iostream>
#include <string>

using namespace std;

class TextBuilder
{
public:
	virtual void Draw(string Text) = 0;
};

class TextBuilder1 :public TextBuilder
{
public:
	void Draw(string Text) override
	{
		cout << "Text1: " << Text << endl;
	}
} Text1;

class TextBuilder2 :public TextBuilder
{
public:
	void Draw(string Text) override
	{
		cout << "Text2: " << Text << endl;
	}
} Text2;

void ShowString (TextBuilder &Method, string Text)
{
	Method.Draw(Text);
}

int main()
{
	ShowString(Text1, "Adam");
	ShowString(Text2, "Adam");
	return 0;
} 

Dostaję poprawny wynik. Czy to jest błąd w kompilatorze?? Czy raczej ja czegoś nie rozumiem??

1

http://ideone.com/bAWXMC
pics or it didn't happen

2

Wygląda na to, że klasa bez nazwy nie ma konstruktora (nawet domyślnego), co powoduje że vtable nie zostaje prawidłowo zainicjowane...

Wydaje mi się że to jest raczej UB a nie bug w Visualu. Jakiś szpec od standardu musiałby się wypowiedzieć...

Shalom napisał(a)

pics or it didn't happen
happen, happen.

0

@Shalom Dobra udało mi się :D
Poprawny wynik: http://4programmers.net/Pastebin/3355
Błędny wynik: http://4programmers.net/Pastebin/3356

3

Ten kod powinien działać.

Jak widać nie naprawili tego od VS2008: http://stackoverflow.com/questions/2082339/virtual-tables-on-anonymous-classes

1

no tylko teraz im to zgłosić :D najciekawsze jest to że zgłaszanie błędów również nie działa: https://connect.microsoft.com/VisualStudio/Feedback :D i teraz pytanie jak zgłosić błąd w zgłaszaniu błedów :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1