Sorry,
I was playing with c++ and i did something strange and it worked, someone knows why it worked.
Main.cpp
#include <iostream>
#include "c1.h"
using namespace std;
Classe c(){
Classe v();
}
int main(){
cout << c().x;
return 0;
}
c1.h
#include <iostream>
using namespace std;
class Classe{
public:
Classe(){
x = 100;;
}
int x;
};
So, why i can call c().x if the function Classe c(){Classe v()} dont return the variable?
No warnings with and without -wall.
if i do return v on the function it dont compile:
Error message with return:
main.cpp: In function ‘Classe c()’:
main.cpp:8: error: conversion from ‘Classe (*)()’ to non-scalar type ‘Classe’ requested
i think that is by accident, your compiler should have warned you that c() does not return a value.
accidentally the register used to return a value from a function happened to be loaded with the address of the local variable allocated within that function.
And, btw, your declaration of local v() in the function c() does not declare a variable of type
Classe, but a function taking no arguments, returning an object ofClasse.