c++成员函数默认参数
2015-04-17 10:44
260 查看
在C++中,可以为参数指定默认值。在函数调用时没有指定与形参相对应的实参时, 就自动使用默认参数。
默认参数的语法与使用:
(1)在【函数声明】或
但应该尽量在声明是定义默认参数【.h文件】这是给外部的借口。
或者有特殊要求,才在【.cpp文件】中定义
(2)在函数调用时,省略部分或全部参数。这时可以用默认参数来代替。
(3)默认参数应该从右向左
(4)需要注意虚函数,多态时候,default parametrs 问题,默认参数并不会“多态”。
编译期决定??
注意:
(1)默认参数只可在函数声明中设定一次。只有在没有函数声明时,才可以在函数定义中设定。(此句意为存在函数声明和定义两部分的时候。 定义或者声明只有一处)
(2)注意函数重载,防止二义性
c++成员函数默认参数 多文件编写注意
.h file
.cpp file
默认参数的语法与使用:
(1)在【函数声明】或
【定义】时,直接对参数赋值。这就是默认参数的两种方式;
但应该尽量在声明是定义默认参数【.h文件】这是给外部的借口。
或者有特殊要求,才在【.cpp文件】中定义
(2)在函数调用时,省略部分或全部参数。这时可以用默认参数来代替。
(3)默认参数应该从右向左
(4)需要注意虚函数,多态时候,default parametrs 问题,默认参数并不会“多态”。
编译期决定??
注意:
(1)默认参数只可在函数声明中设定一次。只有在没有函数声明时,才可以在函数定义中设定。(此句意为存在函数声明和定义两部分的时候。 定义或者声明只有一处)
(2)注意函数重载,防止二义性
c++成员函数默认参数 多文件编写注意
.h file
#ifndef PARAMETER_H_ #define PARAMETER_H_ class cabc { private: char m_abc; static int def; public: //error: invalid use of non-static data member 'cabc::m_abc' //void print(char a=m_abc,char* str) const; //error //void print(int a=5); void print(char* str1,char* str=NULL); void print(int i=def) const; int add(int i=def); cabc(char c='a'); int set_value(int i); ~cabc(); }; #endif
.cpp file
#include <iostream> #include "parameters.h" using std::cout; using std::endl; int cabc::def=100; //缺省参数放在声明写 //ERROR:like the followed /*void cabc::print(char a,char*) const //error { cout<<"m_abc="<<m_abc<<endl; }*/ //error: 'def' was not declared in this scope //error: non-member function 'void print(int)' cannot have cv-qualifier //void print(int i=def) const //error //after previous specification in 'void def::print(int)' //void def::print(int a=100) //{ // cout<<"a= "<<a<<endl; //} void cabc::print(int i) const { cout<<"def="<<def<<endl; } int cabc::add(int i) { return ++i; } int cabc::set_value(int i=1) { return def=i; } void cabc::print(char* str1,char* str) { cout<<"str= "<<str<<endl; printf("\n"); printf("str: %p\n",str); } cabc::cabc(char c):m_abc(c) { cout<<"cabc's constructor\n"; } cabc::~cabc() { cout<<"cabc's destructor\n"; } int main() { cabc c1; char str[]="abc"; //c1.print(str); c1.print(); cout<<"num= "<<c1.add()<<endl; cout<<"c1.set_value()= "<<c1.set_value()<<endl; c1.print(); c1.print((char*)NULL); }
#include <iostream> using namespace std; class Root { public : virtual void print(int arg=11) { cout<<"Root\n"; cout<<arg<<endl; } // do something }; class RootDerived : public Root { public : // overwrit the function ,the default argument will be problematic virtual void print(int arg=33) { cout<<"RootDerived\n"; cout<<arg<<endl; } // do something }; int main() { Root *pr = new RootDerived; pr-> print(); // do something }
相关文章推荐
- C++学习总结(十一)——类的静态函数,成员函数的默认参数,类中包含引用和常量
- c++的默认成员函数
- C++中的空类,默认产生哪些类成员函数?
- C++中的四个默认成员函数与运算符重载详解
- C++空类默认有哪些成员函数?
- C++中的空类,编译器默认可以产生哪些成员函数
- c++中函数的默认参数(5)
- C++ 空类,默认产生哪些成员函数
- C/C++中默认参数在哪指定?函数声明还是定义?默认参数需放在右边
- C++中的空类,默认产生哪些类成员函数?
- C++中的空类,编译器默认可以产生哪些成员函数
- C++关于参数为const引用类型的函数调用参数成员函数的一个问题
- c++ const: const 变量, const 输入参数, const 返回值, const 成员函数
- C++中的空类,默认产生哪些类成员函数?
- c++基础--c++默认生成的成员函数
- C++中一个空类默认产生的成员函数
- C++ 空类,默认产生哪些成员函数
- C++默认成员函数使用说明
- C++中的空类,默认产生的类成员函数
- C++中空类有多少个默认成员函数