Tech Kaizen

passion + usefulness = success .. change is the only constant in life

Search this Blog:

Copy Constructors , Assignment Operator & Derived Classes - Copying parent class members

Copy Constructors and Derived Classes - Copying parent class members



The default constructor for a derived class calls the base class default constructor implicitly, without an explicit call. However, the copy constructor for a derived class does not call the copy constructor of the base class --- it calls the default zero-argument constructor for the base class.



class Base

{

public:

Base() { cout << "Base constructor\n"; } Base(Base &b) { cout << "Base COPY constructor\n"; } }; class Derived : public Base { public: Derived(Derived &b) { cout << "Derived COPY constructor\n"; } }; main() { Derived d1; Derived d2(d1); // copy constructor } This is not what we usually require; more typically a derived class copy constructor will explicitly invoke the base class copy constructor using the special syntax for passing arguments to a base class. class Base { public: Base(Base &b) { cout << "Base COPY constructor\n"; } }; class Derived : public Base { Derived(Derived &b) : Base(b) { cout << "Derived COPY constructor\n"; } }; The Derived object ``b" is passed to the Base class copy constructor. Note that this involves converting the Derived& reference to a Base& reference --- recall that conversion of a pointer/reference is allowed from Derived down to base, but not the reverse.





Assignment Operators and Derived Classes - Copying parent class members



Similarly to the problem with copy constructors in derived classes, the assignment operator in derived classes does not implicitly call the base class assignment operator. In fact, by default, the private data of the base class will not be modified by the derived class assignment operator.



The solution is an explicit call to the Base class assignment operator which can be achieved via the ``this" special pointer, and a type cast to a reference to a reference to the Base class.



void operator=(Derived &d)

{

(Base&)*this = d; // EXPLICIT CALL // The above copies the Base class data

// Now copy the derived class data ....

}



There are a few other alternative methods of explicit calls, such as:



void operator=(Derived &d)

{

(*this).Base::operator=(d); // EXPLICIT CALL

}



(or)



If there is a parent (base) class, those fields must also be copied.

You can accomplish this with the following cryptic statement,



this->Parent::operator=(source); //where Parent is the name of the base class.



//--- file Parent.h

class Parent {...}; // declaration of base class



//--- file Child.h

#include "Parent.h"

class Child : public Parent { // declaration of derived class

public:

Child& Child::operator=(const Child& source);

};//end class Child



//--- file Child.cpp

#include "Child.h"

Child& Child::operator=(const Child& source) {

if (this != &source) {

this->Parent::operator=(source);

. . . // copy all our own fields here.

}

return *this;

}//end operator=





Methods that are implicitly generated by the compiler if they are not explicitly defined are:



a. Default constructor (C::C())



b. Copy constructor (C::C (const C& rhs))



c. Destructor (C::~C())



d. Assignment operator (C& C::operator= (const C& rhs))



e. Address-of operator (C* C::operator&())



f. Address-of operator (const C* C::operator&() const;)





Private Copy Constructor:



1. Use private copy constructor and assignment operator to avoid object copyingIf you don't want users of your class to be able to assign objects of its type (password string objects are a good example), you can declare a private assignment operator and copy constructor.



Please note that the compiler-synthesized copy constructor and assignment operator are public, therefore, you have to define them explicitly as private members in this case.



2. To make sure we can not pass objects by value only by reference.



Links:



http://www.cs.jcu.edu.au/Subjects/cp3120/1996/Lectures/c++/node92.html



C/C++ Memory Corruption And Memory Leaks - http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html





Posted by Krishna Kishore Koney
Labels: C++ PROGRAMMING

JAVA CODE REVIEW CHECKLIST

JAVA Code Review CheckList



Error Handling

1. Does the code comply with the accepted Exception Handling Conventions.

a. We need to expand our notion of Exception Handling Conventions.

b. Some method in the call stack needs to handle the exception, so that we don’t display that exception stacktrace to the end user.

2. Does the code make use of exception handling?

a. Exception handling should be consistent throughout the system.

3.Does the code simply catch exceptions and log them?

a. Code should handle exceptions, not just log them.

4.Does the code catch general exception (java.lang.Exception)?

a. Catching general exceptions is commonly regarded as “bad practice”.

5.Does the code correctly impose conditions for “expected” values?

a.For instance, if a method returns null, does the code check for null?

The following code should check for null

Person person = Context.getPersonService().getPerson(personId);

person.getAddress().getStreet();

What should be our policy for detecting null references?

6.Does the code test all error conditions of a method call?

a. Make sure all possible values are tested.

b.Make sure the JUnit test covers all possible values.

Security

1. Does the code appear to pose a security concern?

a. Passwords should not be stored in the code. In fact, we have adopted a policy in which we store passwords in runtime properties files.

b.Connect to other systems securely – i.e. use HTTPS instead of HTTP where possible.

Thread Safeness

1. Does the code practice thread safeness?

a. If objects can be accessed by multiple threads at one time, code altering global variables (static variables) should be enclosed using a synchronization mechanism (synchronized).

b. In general, controllers / servlets should not use static variables.

c. Use synchronization on the smallest unit of code possible. Using synchronization can cause a huge performance penalty, so you should limit its scope by synchronizing only the code that needs to be thread safe.

d. Write access to static variable should be synchronized, but not read access.

e. Even if servlets/controllers are thread-safe, multiple threads can access HttpSession attributes at the same time, so be careful when writing to the session.

f. Use the volatile keyword to warn that compiler that threads may change an instance or class variable – tells compiler not to cache values in register.

g. Release locks in the order they were obtained to avoid deadlock scenarios.

2. Does the code avoid deadlocks?

a. I’m not entirely sure how to detect a deadlock, but we need to make sure we acquire/release locks in a manner that does not cause contention between threads. For instance, if Thread A acquires Lock #1, then Lock #2, then Thread B should not acquire Lock #2, then Lock #1.

b.Avoid calling synchronized methods within synchronized methods.

Resource Leaks

1. Does the code release resources?

a. Close files, database connections, HTTP connections, etc.

2. Does the code release resources more than once?

a. This will sometimes cause an exception to be thrown.

3. Does the code use the most efficient class when dealing with certain resources?

a. For instance, buffered input / output classes.

Miscellaneous:

1.Make sure that we are using StringBuffer if we want to change the contents of a String



2.Always use “.equals” instead of “==” during Object Comparision



3.Use wait()/notify() instead of sleep()

Links:

Checklist: Java Code Review -
http://snap.uci.edu/viewXmlFile.jsp?resourceID=1529

http://www.javaworld.com/javaworld/javatips/jw-javatip88.html

http://undergraduate.csse.uwa.edu.au/units/CITS2220/assign2/JavaInspectionCheckList.pdf

http://www.cs.toronto.edu/~sme/CSC444F/handouts/java_checklist.pdf

http://www.deaded.com/staticpages/index.php/codereviewprocess



Posted by Krishna Kishore Koney
Labels: CODE REVIEWS
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

The Verge - YOUTUBE

Loading...

Hard Fork Podcast

Loading...

Dwarkesh Patel Podcast

Loading...

SemiAnalysis Podcast (Dylan Patel)

Loading...

Andrej Karpathy Youtube Channel

Loading...

Microsoft Research

Loading...

Hugging Face - Blog

Loading...

AI at Wharton

Loading...

Stanford Online

Loading...

MIT OpenCourseWare - YOUTUBE

Loading...

NPTEL IISC BANGALORE - YOUTUBE

Loading...

HackerRank - YOUTUBE

Loading...

FREE CODE CAMP - YOUTUBE

Loading...

BYTE BYTE GO - YOUTBUE

Loading...

GAURAV SEN INTERVIEWS - YOUTUBE

Loading...

Tanay Pratap - YOUTUBE

Loading...

Ashish Pratap Singh - YOUTUBE

Loading...

Kantan Coding - YOUTUBE

Loading...

SUCCESS IN TECH INTERVIEWS - YOUTUBE

Loading...

IGotAnOffer: Engineering - YOUTUBE

Loading...

DEEPLEARNING AI - YOUTUBE

Loading...

MIT News - Artificial intelligence

Loading...
My photo
Krishna Kishore Koney
View my complete profile
" It is not the strongest of the species that survives nor the most intelligent that survives, It is the one that is the most adaptable to change "

View krishna kishore koney's profile on LinkedIn


Failure is not falling down, it is not getting up again. Success is the ability to go from failure to failure without losing your enthusiasm.

Where there's a Will, there's a Way. Keep on doing what fear you, that is the quickest and surest way to to conquer it.

Vision is the art of seeing what is invisible to others. For success, attitude is equally as important as ability.

Monthly Blog Archives

  • ▼  2026 (7)
    • ▼  July (2)
      • REST vs GraphQL
      • Nautobot: Opensource Network Source of Truth (NSoT...
    • ►  May (1)
    • ►  April (1)
    • ►  March (3)
  • ►  2025 (4)
    • ►  October (1)
    • ►  August (1)
    • ►  May (1)
    • ►  April (1)
  • ►  2024 (18)
    • ►  December (1)
    • ►  October (2)
    • ►  September (5)
    • ►  August (10)
  • ►  2022 (2)
    • ►  December (2)
  • ►  2021 (2)
    • ►  April (2)
  • ►  2020 (18)
    • ►  November (1)
    • ►  September (8)
    • ►  August (1)
    • ►  June (8)
  • ►  2019 (18)
    • ►  December (1)
    • ►  November (2)
    • ►  September (3)
    • ►  May (8)
    • ►  February (1)
    • ►  January (3)
  • ►  2018 (3)
    • ►  November (1)
    • ►  October (1)
    • ►  January (1)
  • ►  2017 (2)
    • ►  November (1)
    • ►  March (1)
  • ►  2016 (5)
    • ►  December (1)
    • ►  April (3)
    • ►  February (1)
  • ►  2015 (15)
    • ►  December (1)
    • ►  October (1)
    • ►  August (2)
    • ►  July (4)
    • ►  June (2)
    • ►  May (3)
    • ►  January (2)
  • ►  2014 (13)
    • ►  December (1)
    • ►  November (2)
    • ►  October (4)
    • ►  August (5)
    • ►  January (1)
  • ►  2013 (5)
    • ►  September (2)
    • ►  May (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (19)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  July (1)
    • ►  June (6)
    • ►  May (1)
    • ►  April (2)
    • ►  February (3)
    • ►  January (2)
  • ►  2011 (20)
    • ►  December (5)
    • ►  August (2)
    • ►  June (6)
    • ►  May (4)
    • ►  April (2)
    • ►  January (1)
  • ►  2010 (41)
    • ►  December (2)
    • ►  November (1)
    • ►  September (5)
    • ►  August (2)
    • ►  July (1)
    • ►  June (1)
    • ►  May (8)
    • ►  April (2)
    • ►  March (3)
    • ►  February (5)
    • ►  January (11)
  • ►  2009 (113)
    • ►  December (2)
    • ►  November (5)
    • ►  October (11)
    • ►  September (1)
    • ►  August (14)
    • ►  July (5)
    • ►  June (10)
    • ►  May (4)
    • ►  April (7)
    • ►  March (11)
    • ►  February (15)
    • ►  January (28)
  • ►  2008 (61)
    • ►  December (7)
    • ►  September (6)
    • ►  August (1)
    • ►  July (17)
    • ►  June (6)
    • ►  May (24)
  • ►  2006 (7)
    • ►  October (7)

Blog Archives Categories

  • .NET DEVELOPMENT (38)
  • 5G (5)
  • AI (Artificial Intelligence) (16)
  • AI/ML (10)
  • ANDROID DEVELOPMENT (7)
  • BIG DATA ANALYTICS (6)
  • C PROGRAMMING (7)
  • C++ PROGRAMMING (24)
  • CAREER MANAGEMENT (6)
  • CHROME DEVELOPMENT (2)
  • CLOUD COMPUTING (47)
  • CODE REVIEWS (3)
  • CYBERSECURITY (12)
  • DATA SCIENCE (4)
  • DATABASE (14)
  • DESIGN PATTERNS (9)
  • DEVICE DRIVERS (5)
  • DIY (3)
  • DOMAIN KNOWLEDGE (14)
  • EDGE COMPUTING (4)
  • EMBEDDED SYSTEMS (9)
  • ENTERPRISE ARCHITECTURE (10)
  • IMAGE PROCESSING (3)
  • INTERNET OF THINGS (2)
  • J2EE PROGRAMMING (10)
  • KERNEL DEVELOPMENT (6)
  • KUBERNETES (20)
  • LATEST TECHNOLOGY (25)
  • LINUX (9)
  • MAC OPERATING SYSTEM (2)
  • MOBILE APPLICATION DEVELOPMENT (14)
  • PORTING (4)
  • PYTHON PROGRAMMING (6)
  • RESEARCH AND DEVELOPMENT (1)
  • SCRIPTING LANGUAGES (8)
  • SERVICE ORIENTED ARCHITECTURE (SOA) (10)
  • SOFTWARE DESIGN (13)
  • SOFTWARE QUALITY (5)
  • SOFTWARE SECURITY (24)
  • SYSTEM and NETWORK ADMINISTRATION (4)
  • SYSTEM PROGRAMMING (4)
  • TECHNICAL MISCELLANEOUS (32)
  • TECHNOLOGY INTEGRATION (5)
  • TEST AUTOMATION (5)
  • UNIX OPERATING SYSTEM (4)
  • VC++ PROGRAMMING (44)
  • VIRTUALIZATION (8)
  • WEB PROGRAMMING (8)
  • WINDOWS OPERATING SYSTEM (13)
  • WIRELESS DEVELOPMENT (5)
  • XML (3)

Popular Posts

  • REST vs GraphQL
  • Service Discovery Protocols
  • REST vs SOAP
  • Uninstall Kubernetes on CentOS Shell Script

My Other Blogs/Channels

  • Career Management: Invest in Yourself
  • A la carte: Color your Career
  • Attitude is everything(Telugu language)
  • "Invest in Yourself" Youtube Channel (Telugu language)
WINNING vs LOSING

Hanging on, persevering, WINNING
Letting go, giving up easily, LOSING

Accepting responsibility for your actions, WINNING
Always having an excuse for your actions, LOSING

Taking the initiative, WINNING
Waiting to be told what to do, LOSING

Knowing what you want and setting goals to achieve it, WINNING
Wishing for things, but taking no action, LOSING

Seeing the big picture, and setting your goals accordingly, WINNING
Seeing only where you are today, LOSING

Being determined, unwilling to give up WINNING
Gives up easily, LOSING

Having focus, staying on track, WINNING
Allowing minor distractions to side track them, LOSING

Having a positive attitude, WINNING
having a "poor me" attitude, LOSING

Adopt a WINNING attitude!

Total Pageviews

Who am I

My photo
Krishna Kishore Koney

Blogging is about ideas, self-discovery, and growth. This is a small effort to grow outside my comfort zone.

Most important , A Special Thanks to my parents(Sri Ramachandra Rao & Srimathi Nagamani), my wife(Roja), my lovely daughter (Hansini) and son (Harshil) for their inspiration and continuous support in developing this Blog.

... "Things will never be the same again. An old dream is dead and a new one is being born, as a flower that pushes through the solid earth. A new vision is coming into being and a greater consciousness is being unfolded" ... from Jiddu Krishnamurti's Teachings.

Now on disclaimer :
1. Please note that my blog posts reflect my perception of the subject matter and do not reflect the perception of my Employer.

2. Most of the times the content of the blog post is aggregated from Internet articles and other blogs which inspired me. Due respect is given by mentioning the referenced URLs below each post.

Have a great time

My LinkedIn Profile
View my complete profile

Aryaka Insights

Loading...

Reid Hoffman - YOUTUBE

Loading...

Martin Fowler's Bliki - BLOG

Loading...

The Pragmatic Engineer

Loading...

AI Workshop

Loading...

CYBER SECURITY - YOUTUBE

Loading...

CYBER SECURITY FUNDAMENTALS PROF MESSER - YOUTUBE

Loading...