473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

valid syntax?

I implementing the 'virtual constructor' idiom, I have a retrieve()
method on an interface, which retrieves an object from storage.

I am using this line to cast "up":
MyObject::retrieve()
{
Base * base = Create(object_id) ;
*this = *dynamic_cast<MyObject*>(base) ; //<- this line
...
}

Notes:
i). MyObject derives from Base
ii). Create is a virtual constructor which returns ptr to objects
derived from Base
Sep 26 '07 #1
1 1502
I think the syntax here is valid but might not be what you want.
Dereferencing this and the result of the dynamic cast will invoke the
operator=() on MyObject. If you haven't implemented that then it will
do a bitwise copy. If you meant for this to point to the newly created
object then you want to get rid of 2 asterisks on that line.

Bear in mind as well that dynamic cast can fail. When dynamic casting
pointers a failure will return NULL which you'll then try to
dereference and consequently crash.

Why do you ask if it's valid syntax? What does the compiler say?
On Sep 26, 2:06 pm, Anonymous <no.re...@here.comwrote:
I implementing the 'virtual constructor' idiom, I have a retrieve()
method on an interface, which retrieves an object from storage.

I am using this line to cast "up":

MyObject::retrieve()
{
Base * base = Create(object_id) ;
*this = *dynamic_cast<MyObject*>(base) ; //<- this line
...

}

Notes:
i). MyObject derives from Base
ii). Create is a virtual constructor which returns ptr to objects
derived from Base

Sep 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Hill | last post by:
Is this a valid sql statement: SELECT case field1 when 'first' then 1 when 'second' then 1 else null end FROM mytable
2
by: DFS | last post by:
Version 1: -------------------------------- If Condition = A UPDATE query 1 UPDATE query 2 ELSE IF Condition = B UPDATE query
8
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta...
2
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
5
by: Sriram Rajagopalan | last post by:
Hi, Is the extra comma at the end of an enumerator-list valid according to the C standards? With the gcc compiler the following is valid: enum DAYS {MONDAY, TUESDAY, }day1; gcc does not...
4
by: lizhuo | last post by:
hi all: I reading "C++ Templates: The Complete Guide " Part II: Templates in Depth he write: template<typename T> class B { public: enumE{e1=6,e2=28,e3=496}; virtual void zero(E e = e1);
9
by: Marijn | last post by:
Hey everybody, I was wondering if the following would be a valid JavaScript assignment vor a variable and if anybody knows this to work in IE6 or lower? var someVar = (typeof(someVar) ===...
14
by: paresh | last post by:
Is this the valid C statement. int a,b,c; c = 5; <<< a = b = c; Can anyone throw the light on this. -Paresh
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.