derived

区分好继承类和封闭类,两者有很大的区别

匿名 (未验证) 提交于 2019-12-03 00:21:02
一、继承类 在继承中,子类负责其直接基类的构造,至于如何构造需要看下面; 如何在子类中调用拷贝构造函数,看下面; #include <iostream> #include <bits/stdc++.h> using namespace std ; class Base { private : int _a; public : Base( int a= 0 ):_a(a){ cout << "Base = " <<_a<< " is created." <<endl;} Base( const Base& p):_a(p._a){ cout << "Base = " <<_a<< " is copied." <<endl;} ~Base(){ cout << "Base = " <<_a<< " is erased." <<endl;} }; class Derived: public Base { private : int _b; public : Derived( int a= 0 , int b= 0 ):Base(a),_b(b){ cout << "Derived = " <<_b<< " is created." <<endl;} Derived( const Derived& p):Base(p),_b(p._b){ cout << "Derived = " <<_b

Transaction has ended in trigger. Batch has been aborted. Derived Attribute

吃可爱长大的小学妹 提交于 2019-12-02 02:20:01
I have this trigger : CREATE trigger [dbo].[DeriveTheAge] on [dbo].[Student] after insert,update as begin declare @sid as int; declare @sdate as date; select @sid= [Student ID] from inserted; select @sdate=[Date of Birth] from inserted; commit TRANSACTION if(@sdate is not null) begin update Student set Age=DATEDIFF(YEAR,@sdate,GETDATE()) where [Student ID]=@sid; end print 'Successfully Done' end as it suggests, the trigger automatically calculates the Derived attribute "Age" from the date of birth. But I get this error when I do the insert : (1 row(s) affected) Successfully Done Msg 3609,

Calling base method from derived class

╄→尐↘猪︶ㄣ 提交于 2019-12-01 22:04:41
I have, for example, such class: class Base { public: void SomeFunc() { std::cout << "la-la-la\n"; } }; I derive new one from it: class Child : public Base { void SomeFunc() { // Call somehow code from base class std::cout << "Hello from child\n"; } }; And I want to see: la-la-la Hello from child Can I call method from derived class? Sure: void SomeFunc() { Base::SomeFunc(); std::cout << "Hello from child\n"; } Btw since Base::SomeFunc() is not declared virtual , Derived::SomeFunc() hides it in the base class instead of overriding it, which is surely going to cause some nasty surprises in the

Avoid dynamic_cast with derived classes (Cast Derived class)

旧时模样 提交于 2019-12-01 00:39:33
I am new to C++ and came to a point, where I generate an overhead with classes. I have a QTcpSocket and read messages from it and create objects, for example MessageJoin, MessagePart, MessageUserData etc. I send these objects to my client and display them (+ do some UI updating). Now here comes my problem. I tested a few design techniques but all of them are not that nice: Pass each parameter of a message object in a signal/slot connection to the client - small overhead but not that good-looking Create a method for each Message-Type (messageJoinReceived, messageNoticeReceived etc.) Create one

Avoid dynamic_cast with derived classes (Cast Derived class)

北慕城南 提交于 2019-11-30 20:05:07
问题 I am new to C++ and came to a point, where I generate an overhead with classes. I have a QTcpSocket and read messages from it and create objects, for example MessageJoin, MessagePart, MessageUserData etc. I send these objects to my client and display them (+ do some UI updating). Now here comes my problem. I tested a few design techniques but all of them are not that nice: Pass each parameter of a message object in a signal/slot connection to the client - small overhead but not that good

Get derived type via base class virtual function

半城伤御伤魂 提交于 2019-11-30 19:06:24
I am trying to get the derived type of an object via a base class virtual function. I have written this, which does not compile: struct base { virtual base& get_this() { return *this; } }; struct derived : base { virtual derived& get_this() override { return *this; } void fn(); }; int main () { base* pd = new derived(); derived& x = pd->get_this(); /*ERROR*/ x.fn(); return 0; } ... giving me an error that: I cannot initialize a derived& from a base . Since get_this is virtual, why does pd->get_this() return a base& instead of a derived& ? Thanks in advance! EDIT: Thanks everyone for their

指向成员的指针运算符:. *和- >*

◇◆丶佛笑我妖孽 提交于 2019-11-30 07:09:33
指向成员的指针运算符:. *和- >* ----来自微软technet Visual Studio 2012 expression .* expression expression –>* expression 备注 指向成员的指针运算符,。* 和 – AMP_GT*,返回特定的类成员的值在表达式的左侧指定对象的。 右侧必须指定类的成员。 下面的示例演示如何使用这些运算符: // expre_Expressions_with_Pointer_Member_Operators.cpp // compile with: /EHsc #include <iostream> using namespace std; class Testpm { public: void m_func1() { cout << "m_func1\n"; } int m_num; }; // Define derived types pmfn and pmd. // These types are pointers to members m_func1() and // m_num, respectively. void (Testpm::*pmfn)() = &Testpm::m_func1; int Testpm::*pmd = &Testpm::m_num; int main() { Testpm

Use of “Public” in a derived class declaration?

帅比萌擦擦* 提交于 2019-11-28 18:18:31
Given this base class: class Employee { char* name; int age; public: Employee(char* name); void print(); }; With regards to the "public", what's the difference between this: class Manager : public Employee { EmployeeList employees; public: Manager(char* name, Employee* people); void print(); }; and this: class Manager : Employee { EmployeeList employees; public: Manager(char* name, Employee* people); void print(); }; The default is private inheritance. take this example: class B { }; class D: B { }; uses private inheritance as its the default. This means that D gets all the protected and

Why would the conversion between derived* to base* fails with private inheritance?

蹲街弑〆低调 提交于 2019-11-28 07:29:46
Here is my code - #include<iostream> using namespace std; class base { public: void sid() { } }; class derived : private base { public: void sid() { } }; int main() { base * ptr; ptr = new derived; // error: 'base' is an inaccessible base of 'derived' ptr->sid(); return 0; } This gives a compile time error. error: 'base' is an inaccessible base of 'derived' Since the compiler will try and call the base class sid() why do I get this error? Can someone please explain this. Chubsdad $11.2/4 states- A base class B of N is accessible at R, if an invented public member of B would be a public member

Does a derived class automatically have all the attributes of the base class?

微笑、不失礼 提交于 2019-11-28 05:23:16
There seems to be no good online documentation on this: If I make a derived class, will it automatically have all the attributes of the base class? But what's the BaseClass.__init() for, do you also need to do it to other base class methods? Does BaseClass.__init__() need arguments? If you have arguments for your base class __init__() , are they also used by the derived class, do you need to explicitly set the arguments to the derived classe's __init__() , or set them to BaseClass.__init__() instead? If you implement __init__ in a class derived from BaseClass, then it will overwrite the