473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to cast from list<T*> to list<const T*>

//Consider:
class A { /*...*/ };

template<class T> class list {/*... */ };

void f(const list<const A*> lst) { /*...doesn't change the arg...*/ }

void g(list<A*> lst) {
f(lst); //Intuitively ok, but compiler rejects.
}

/* Compiler says:
error: conversion from `list<A*>' to non-scalar type `list<const A*>'
requested

Cast doesn't help either. What to do?
*/
--
Best regards,
Alex.

PS. To email me, remove "loeschedie s" from the email address given.
Jul 22 '05 #1
2 2388
Alexander Malkis <al************ *****@stone.cs. uni-sb.de> wrote in message news:<c4******* *****@hades.rz. uni-saarland.de>...
//Consider:
class A { /*...*/ };

template<class T> class list {/*... */ };

void f(const list<const A*> lst) { /*...doesn't change the arg...*/ }

void g(list<A*> lst) {
f(lst); //Intuitively ok, but compiler rejects.
}

/* Compiler says:
error: conversion from `list<A*>' to non-scalar type `list<const A*>'
requested

Cast doesn't help either. What to do?
*/


2 things

1) You are working on a list why not use the STL list(or one of the
other containers)?

2) did you really mean -> void f(const list<const A*> lst)
or did you want to do -> void f(const list<const A*>& lst)
?

That said because you are using a template there are two distinct
classes one is list<A*> and the other is list<const A*> which are in
no way related.
My first guess is that you'd need to write a conversion routine that
accepts a T and converts is to a const T.

My first guess seems to be supported by the compiler I'm using (VC++
7)
The intel compiler spits out the following error:
error: no suitable user-defined conversion from "list<A *>" to "const
list<const A *>" exists f(lst); //Intuitively ok, but compiler
rejects.

The M$ compiler spits out the following error:
error C2664: 'f' : cannot convert parameter 1 from 'list<T>' to 'const
list<T>' with [ T=A * ] and [ T=const A * ]
Jul 22 '05 #2
velthuijsen wrote:
1) You are working on a list why not use the STL list(or one of the
other containers)? I have my own functions for lists which are not present in standard
libraries.

2) did you really mean -> void f(const list<const A*> lst)
or did you want to do -> void f(const list<const A*>& lst)
? It's not essential for the matter of conversion.
My first guess is that you'd need to write a conversion routine that
accepts a T and converts is to a const T.

You mean maybe (T*) to (const T*) ?

--
Best regards,
Alex.

PS. To email me, remove "loeschedie s" from the email address given.
Jul 22 '05 #3

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

Similar topics

2
1924
by: Alexander Malkis | last post by:
//Consider: class A { /*...*/ }; template<class T> class list {/*... */ }; void f(const list<const A*> lst) { /*...doesn't change the arg...*/ } void g(list<A*> lst) { f(lst); //Intuitively ok, but compiler rejects. }
6
6652
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
4
52969
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
7
57536
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
45
18848
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
6
1966
by: gsBytes | last post by:
I am working with a specialized graph class, which has a node class and an edge class. In the node class, there is a list of pointers to each edge. Code that uses the graph must be able to see what edges are contained in a node. This is handled by returning a const reference to a list of edge pointers, i.e: const list<Edge*>& Node::getEdges() const { return edges; // list<Edge*> } However, I don't like this because code outside the...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.