先生在范围解析中我们可以在类外定义构造函数吗? [英] Sir in scope resolution can we define constructor outside the class?

查看:124
本文介绍了先生在范围解析中我们可以在类外定义构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请尽快回复我



我尝试了什么:



i试图在类外定义一个构造函数并且它不起作用先生告诉我可以在类外面进行二分法

please reply me as soon as possible

What I have tried:

i have tried to define an constructor outside the class and it does not work sir tell me can it be possible to difine outside the class

推荐答案

不,你不应该这样做,因为它破坏了封装的类模式。如果你需要修改一个类对象,它应该总是使用成员函数。



我认为你必须重新考虑你的软件设计。从这个教程中学习。
No and you shouldnt do it, because it destroyes the class pattern of encapsulation. If you need to modify a class object it should always made with member functions.

I think you must rethink your software design. Learn from this tutorials.


构造函数必须是(根据定义)班上的一员。但是你可以将它的定义放在类之外(声明必须在第一类中):

The contructor must be (by definition) a member of the class. However you can put its definition outside of the class (declaration must be inside the class one):
// ctor.h header file
class A
{
public:
  A(); // just ctor declaration
};






and

// ctor.cpp source file
#include <iostream>
#include "ctor.h"

// this is the ctor definition
A::A(){std::cout << "A()" << std::endl;}

int main()
{
  A a;
}


这篇关于先生在范围解析中我们可以在类外定义构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆