C#
  Home arrow C# arrow Page 5 - SatView: Pointer Perfect, part 4
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

SatView: Pointer Perfect, part 4
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 6
    2004-12-27

    Table of Contents:
  • SatView: Pointer Perfect, part 4
  • Fence Post Errors
  • Dangling Pointers
  • Performance Hit When Initializng
  • Object Slicing

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    SatView: Pointer Perfect, part 4 - Object Slicing


    (Page 5 of 5 )

    It sounds pretty fictional, but have you ever heard of "Object Slicing" in C++? Oh the fun you can have with inheritance and base class usage! Where I complained that pointer casting could show no respect for identity, pointers actually help you save identity in the following case:

    class MyBaseClass {
    public:
    virtual ~MyBaseClass() {}
    virtual void bar() const { (void)printf(“MyBaseClass::bar();\n”); }
    };
    class MyDerivedClass : public MyBaseClass
    {
    public:
      virtual ~MyDerivedClass() {}
    virtual void bar() const { (void)printf(“MyDerivedClass::bar();\n”); }
    };

    /* this will slice */
    void foo(MyBaseClass param) {
      param.bar();
    }

    void test() {
      DerivedClass myObj;
      foo(myObj);
    }

    The test() function calls foo() and passes an object by value. This object is correctly derived from MyBaseClass, but when we pass it to foo() only the copy constructor of MyBaseClass is being called upon! This doesn’t maintain the virtual table and the baseclass is not aware of any virtual derived function it should call upon instead of its own. So the output is “MyBaseClass::bar();”!

    One way to prevent this from happening is by passing the object by reference, making foo() use the same instance leaving the vtable intact:

    /* this will not slice */
    void foo(MyBaseClass *param) {
      param->bar();
    }

    or

    /* neither will this */
    void fooRef(MyBaseClass &param) {
      param.bar();
    }

    Either function will output “MyDerivedClass::bar();”. You see that slicing is nothing more than casting and copying a derived object back to its base object, making it smaller than the original derived version. The usage of pointers or references prevents copies being made and thus prevents possible slicing of objects.

    I hope you found these articles about pointers useful and that you will stick around through the remaining series, all the way up to the SatView application. Any questions can be mailed to jun.nakamura@gmail.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway