C ++ - 如何在声明类之前让函数返回一个对象?

时间:2017-05-03 05:44:33

标签: c++ class oop object types

这对其他人来说可能是常识,但令我惊讶的是,显然C ++编译器按照它们出现在源文件中的顺序声明了Classes,这意味着在类的出现在源文件中之前,您无法构造对象。我的问题是如何让一个类的成员函数在声明类之前返回一个对象?

Here's my PHP code, but it doesn't work,

    <?php
    if(isset($_POST['submit'])){
        $name=$_POST['full_name'];
        $email=$_POST['email'];
        $phone=$_POST['telephone'];
        $msg=$_POST['message'];

        $to='xxxxxxxxx@gmail.com';
        $subject='Call Back Form Submission';
        $message="Name: ".$name."\n"."Phone: ".$phone."\n". "Wrote the following: "."\n\n".$msg;
        $headers="From: ".$email;

        if(mail($to, $subject, $message, $headers)){
            echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly!</h1>";            
        }
        else{
            echo "Something went wrong!";
        }
    }

?>

1 个答案:

答案 0 :(得分:1)

你可以这样做

class A;                 // A forward declaration. A is an incomplete type at this point

class B
{
    A a();               // Okay to declare a function with incomplete return type
                         // but it would be an error to try defining it here
};

class A 
{
    B b();
};                       // A is a complete type now

A B::a() { return A(); } // Can define B::a() now since the return type is complete
B A::b() { return B(); }

编辑:如果没有前瞻声明,就没有办法做到这一点。